​力扣解法汇总791. 自定义字符串排序

news/2024/7/7 20:09:54

 目录链接:

力扣编程题-解法汇总_分享+记录-CSDN博客

GitHub同步刷题项目:

https://github.com/September26/java-algorithms

原题链接:力扣


描述:

给定两个字符串 order 和 s 。order 的所有单词都是 唯一 的,并且以前按照一些自定义的顺序排序。

对 s 的字符进行置换,使其与排序的 order 相匹配。更具体地说,如果在 order 中的字符 x 出现字符 y 之前,那么在排列后的字符串中, x 也应该出现在 y 之前。

返回 满足这个性质的 s 的任意排列 

示例 1:

输入: order = "cba", s = "abcd"
输出: "cbad"
解释: 
“a”、“b”、“c”是按顺序出现的,所以“a”、“b”、“c”的顺序应该是“c”、“b”、“a”。
因为“d”不是按顺序出现的,所以它可以在返回的字符串中的任何位置。“dcba”、“cdba”、“cbda”也是有效的输出。

示例 2:

输入: order = "cbafg", s = "abcd"
输出: "cbad"

提示:

  • 1 <= order.length <= 26
  • 1 <= s.length <= 200
  • order 和 s 由小写英文字母组成
  • order 中的所有字符都 不同

解题思路:

* 解题思路:
* 我的思路是这样的,首先把order转成map,然后遍历s,如果map中存在,则次数+1,并且把对应位置的字符改成'0'。
* 然后再次遍历s,如果对应的字符为'0',则说明该字符是需要排序的,从map中按照顺序依次找出次数>0的进行替换。

代码:

public class Solution791 {

    public String customSortString(String order, String s) {
        Map<Character, Integer> timesMap = new HashMap<>();
        char[] chars1 = order.toCharArray();
        for (Character c : chars1) {
            timesMap.put(c, 0);
        }

        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            Integer integer = timesMap.get(aChar);
            if (integer == null) {
                continue;
            }
            timesMap.put(aChar, integer + 1);
            chars[i] = '0';
        }
        int index = 0;
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            if (aChar != '0') {
                continue;
            }
            char c = 0;
            Integer integer = 0;
            while (true) {
                c = chars1[index];
                integer = timesMap.get(c);
                if (integer == 0) {
                    index++;
                    continue;
                }
                break;
            }
            chars[i] = c;
            timesMap.put(c, integer - 1);
        }
        return new String(chars);
    }
}


http://lihuaxi.xjx100.cn/news/157319.html

相关文章

vue3 + vite + windicss 基础使用

windicss vscode 提示插件 WindiCSS IntelliSense 下载 npm i -D vite-plugin-windicss windicss 配置 vite.config.ts import WindiCSS from "vite-plugin-windicss";export default {plugins: [WindiCSS()], };main.ts import "virtual:windi.css&quo…

安卓讲课笔记5.4 单选按钮和复选框

文章目录零、本讲学习目标一、导入新课二、新课讲解&#xff08;一&#xff09;单选按钮组1、继承关系图2、常用属性3、设置事件监听器4、注意事项5、重要方法&#xff08;二&#xff09;单选按钮1、继承关系图2、常用方法3、设置事件监听器4、注意事项&#xff08;三&#xff…

简单两步,使用 cache 加快极狐GitLab CI/CD 构建速度

本文来自&#xff1a; 李发富 极狐(GitLab) 高级技术支持工程师 A&#xff1a;CI/CD 执行好慢呐&#xff0c;每个 Job 执行的时候都得去下载外部依赖&#xff0c;好烦&#xff01; B&#xff1a; 这你就自寻烦恼了吧&#xff0c;其实可以用 cache 来避免这种状况。 A&#xf…

【Designing ML Systems】第 9 章 :生产中的持续学习和测试

&#x1f50e;大家好&#xff0c;我是Sonhhxg_柒&#xff0c;希望你看完之后&#xff0c;能对你有所帮助&#xff0c;不足请指正&#xff01;共同学习交流&#x1f50e; &#x1f4dd;个人主页&#xff0d;Sonhhxg_柒的博客_CSDN博客 &#x1f4c3; &#x1f381;欢迎各位→点赞…

FFmpeg之Makefile流程分析

1. 核心目标依赖关系 在FFMpeg下执行make&#xff0c;会找到第一个目标all&#xff0c;那么整个编译最终目标就是要完成All目标。我们先找一下all目标的所有依赖 依赖一&#xff1a;all-yes 根目录下的Makefile # first so "all" becomes default target all: all-…

Android GLide图片加载的几种常用方法

前言 Glide库是用来实现图片加载的框架&#xff0c;功能强大且易使用&#xff0c;深受大家喜爱。 缓存浅析 为啥要做缓存&#xff1f; android默认给每个应用只分配16M的内存&#xff0c;所以如果加载过多的图片&#xff0c;为了 防止内存溢出 &#xff0c;应该将图片缓存起…

大一新生HTML期末作业 学生个人网页设计作业 HTML5响应式个人简历网站模板 web前端网页制作课作业

&#x1f389;精彩专栏推荐&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb; ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业…

Scatter plot with histograms

Scatter plot with histograms https://matplotlib.org/stable/gallery/lines_bars_and_markers/scatter_hist.html#sphx-glr-gallery-lines-bars-and-markers-scatter-hist-py 说白了就是搞三个subplot Start with a square Figure. fig plt.figure(figsize(6, 6)) Add …