From fb61849ed9a3e76a2d94faf33ff17f3bbab56586 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Sun, 13 Nov 2022 13:04:11 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20add=20791?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "Index/\346\236\204\351\200\240.md" | 1 + "Index/\346\250\241\346\213\237.md" | 1 + ...10\344\270\255\347\255\211\357\274\211.md" | 110 ++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 "LeetCode/791-800/791. \350\207\252\345\256\232\344\271\211\345\255\227\347\254\246\344\270\262\346\216\222\345\272\217\357\274\210\344\270\255\347\255\211\357\274\211.md" diff --git "a/Index/\346\236\204\351\200\240.md" "b/Index/\346\236\204\351\200\240.md" index f72788de..fc81a371 100644 --- "a/Index/\346\236\204\351\200\240.md" +++ "b/Index/\346\236\204\351\200\240.md" @@ -7,6 +7,7 @@ | [667. 优美的排列 II](https://leetcode.cn/problems/beautiful-arrangement-ii/) | [LeetCode 题解链接](https://leetcode.cn/problems/beautiful-arrangement-ii/solution/by-ac_oier-lyns/) | 中等 | 🤩🤩🤩🤩🤩 | | [761. 特殊的二进制序列](https://leetcode.cn/problems/special-binary-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/special-binary-string/solution/by-ac_oier-cz6h/) | 困难 | 🤩🤩🤩🤩🤩 | | [782. 变为棋盘](https://leetcode.cn/problems/transform-to-chessboard/) | [LeetCode 题解链接](https://leetcode.cn/problems/transform-to-chessboard/solution/by-ac_oier-vf1m/) | 困难 | 🤩🤩🤩🤩 | +| [791. 自定义字符串排序](https://leetcode.cn/problems/custom-sort-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/custom-sort-string/solution/by-ac_oier-ali0/) | 中等 | 🤩🤩🤩🤩🤩 | | [899. 有序队列](https://leetcode.cn/problems/orderly-queue/) | [LeetCode 题解链接](https://leetcode.cn/problems/orderly-queue/solution/by-ac_oier-443m/) | 困难 | 🤩🤩🤩 | | [942. 增减字符串匹配](https://leetcode.cn/problems/di-string-match/) | [LeetCode 题解链接](https://leetcode.cn/problems/di-string-match/solution/by-ac_oier-pvjk/) | 简单 | 🤩🤩🤩🤩🤩 | | [961. 在长度 2N 的数组中找出重复 N 次的元素](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solution/by-ac_oier-bslq/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index 03513391..8b3b23f9 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -104,6 +104,7 @@ | [766. 托普利茨矩阵](https://leetcode-cn.com/problems/toeplitz-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/toeplitz-matrix/solution/cong-ci-pan-du-qu-cheng-ben-fen-xi-liang-f20w/) | 简单 | 🤩🤩🤩 | | [769. 最多能完成排序的块](https://leetcode.cn/problems/max-chunks-to-make-sorted/) | [LeetCode 题解链接](https://leetcode.cn/problems/max-chunks-to-make-sorted/solution/by-ac_oier-4uny/) | 中等 | 🤩🤩🤩🤩🤩 | | [788. 旋转数字](https://leetcode.cn/problems/rotated-digits/) | [LeetCode 题解链接](https://leetcode.cn/problems/rotated-digits/solution/by-ac_oier-9qpw/) | 中等 | 🤩🤩🤩🤩 | +| [791. 自定义字符串排序](https://leetcode.cn/problems/custom-sort-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/custom-sort-string/solution/by-ac_oier-ali0/) | 中等 | 🤩🤩🤩🤩🤩 | | [794. 有效的井字游戏](https://leetcode-cn.com/problems/valid-tic-tac-toe-state/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/valid-tic-tac-toe-state/solution/gong-shui-san-xie-fen-qing-kuang-tao-lun-pikn/) | 中等 | 🤩🤩🤩🤩 | | [796. 旋转字符串](https://leetcode-cn.com/problems/rotate-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/rotate-string/solution/by-ac_oier-bnkx/) | 简单 | 🤩🤩🤩 | | [804. 唯一摩尔斯密码词](https://leetcode-cn.com/problems/unique-morse-code-words/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/unique-morse-code-words/solution/by-ac_oier-a9hv/) | 简单 | 🤩🤩🤩 | diff --git "a/LeetCode/791-800/791. \350\207\252\345\256\232\344\271\211\345\255\227\347\254\246\344\270\262\346\216\222\345\272\217\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/791-800/791. \350\207\252\345\256\232\344\271\211\345\255\227\347\254\246\344\270\262\346\216\222\345\272\217\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..529379ef --- /dev/null +++ "b/LeetCode/791-800/791. \350\207\252\345\256\232\344\271\211\345\255\227\347\254\246\344\270\262\346\216\222\345\272\217\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -0,0 +1,110 @@ +### 题目描述 + +这是 LeetCode 上的 **[791. 自定义字符串排序](https://leetcode.cn/problems/custom-sort-string/solution/by-ac_oier-ali0/)** ,难度为 **中等**。 + +Tag : 「构造」、「模拟」 + + + +给定两个字符串 `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` 中的所有字符都 不同 + +--- + +### 构造 + +根据题意进行模拟即可:起始先使用大小为 $C = 26$ 的数组 `cnts` 对 `s` 的所有字符进行词频统计,随后根据 `order` 的优先级进行构造。 + +若字符 `x` 在 `order` 中排于 `y` 前面,则先往答案追加 `cnts[x]` 个字符 `x`,再往答案追加 `cnts[y]` 个字符 `y`,并更新对应词频,最后将仅出现在 `s` 中的字符追加到答案尾部。 + +Java 代码: +```Java +class Solution { + public String customSortString(String order, String s) { + int[] cnts = new int[26]; + for (char c : s.toCharArray()) cnts[c - 'a']++; + StringBuilder sb = new StringBuilder(); + for (char c : order.toCharArray()) { + while (cnts[c - 'a']-- > 0) sb.append(c); + } + for (int i = 0; i < 26; i++) { + while (cnts[i]-- > 0) sb.append((char)(i + 'a')); + } + return sb.toString(); + } +} +``` +TypeScript 代码: +```TypeScript +function customSortString(order: string, s: string): string { + const cnts = new Array(26).fill(0) + for (const c of s) cnts[c.charCodeAt(0) - 'a'.charCodeAt(0)]++ + let ans = '' + for (const c of order) { + while (cnts[c.charCodeAt(0) - 'a'.charCodeAt(0)]-- > 0) ans += c + } + for (let i = 0; i < 26; i++) { + while (cnts[i]-- > 0) ans += String.fromCharCode(i + 'a'.charCodeAt(0)); + } + return ans +} +``` +Python 代码: +```Python +class Solution: + def customSortString(self, order: str, s: str) -> str: + cnts = [0] * 26 + for c in s: + cnts[ord(c) - ord('a')] += 1 + ans = '' + for c in order: + num = ord(c) - ord('a') + if cnts[num] > 0: + ans += c * cnts[num] + cnts[num] = 0 + for i in range(26): + if cnts[i] > 0: + ans += chr(i + ord('a')) * cnts[i] + return ans +``` +* 时间复杂度:$O(n + m)$ +* 空间复杂度:$O(C)$,其中 $C = 26$ 为字符集大小 + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.791` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +