diff --git a/solution/0700-0799/0761.Special Binary String/README.md b/solution/0700-0799/0761.Special Binary String/README.md index a3c759e61c725..7e34093379f80 100644 --- a/solution/0700-0799/0761.Special Binary String/README.md +++ b/solution/0700-0799/0761.Special Binary String/README.md @@ -53,13 +53,13 @@ tags: ### 方法一:递归 + 排序 -我们可以把特殊的二进制序列看作“有效的括号”,$1$ 代表左括号,$0$ 代表右括号。例如,"11011000" 可以看作:"(()(()))"。 +我们可以把特殊的二进制序列看作“有效的括号”,其中 $1$ 代表左括号,而 $0$ 代表右括号。例如,"11011000" 可以看作:"(()(()))"。 交换两个连续非空的特殊子串,相当于交换两个相邻的有效括号,我们可以使用递归来解决这个问题。 我们将字符串 $s$ 中的每个“有效的括号”都看作一部分,递归处理,最后进行排序,得到最终答案。 -时间复杂度 $O(n^2)$。 +时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。 @@ -113,7 +113,9 @@ class Solution { class Solution { public: string makeLargestSpecial(string s) { - if (s == "") return s; + if (s == "") { + return s; + } vector ans; int cnt = 0; for (int i = 0, j = 0; i < s.size(); ++i) { @@ -154,6 +156,31 @@ func makeLargestSpecial(s string) string { } ``` +#### TypeScript + +```ts +function makeLargestSpecial(s: string): string { + if (s.length === 0) { + return ''; + } + + const ans: string[] = []; + let cnt = 0; + + for (let i = 0, j = 0; i < s.length; ++i) { + cnt += s[i] === '1' ? 1 : -1; + if (cnt === 0) { + const t = '1' + makeLargestSpecial(s.substring(j + 1, i)) + '0'; + ans.push(t); + j = i + 1; + } + } + + ans.sort((a, b) => b.localeCompare(a)); + return ans.join(''); +} +``` + diff --git a/solution/0700-0799/0761.Special Binary String/README_EN.md b/solution/0700-0799/0761.Special Binary String/README_EN.md index bc9c0b47cfe6b..8d949b712aee2 100644 --- a/solution/0700-0799/0761.Special Binary String/README_EN.md +++ b/solution/0700-0799/0761.Special Binary String/README_EN.md @@ -62,7 +62,15 @@ This is the lexicographically largest string possible after some number of swaps -### Solution 1 +### Solution 1: Recursion + Sorting + +We can treat the special binary sequence as "valid parentheses", where $1$ represents an opening parenthesis and $0$ represents a closing parenthesis. For example, "11011000" can be viewed as "(()(()))". + +Swapping two consecutive non-empty special substrings is equivalent to swapping two adjacent valid parentheses. We can use recursion to solve this problem. + +We treat each "valid parenthesis" in string $s$ as a part, process it recursively, and finally sort them to get the final answer. + +The time complexity is $O(n^2)$, and the space complexity is $O(n)$, where $n$ is the length of string $s$. @@ -116,7 +124,9 @@ class Solution { class Solution { public: string makeLargestSpecial(string s) { - if (s == "") return s; + if (s == "") { + return s; + } vector ans; int cnt = 0; for (int i = 0, j = 0; i < s.size(); ++i) { @@ -157,6 +167,31 @@ func makeLargestSpecial(s string) string { } ``` +#### TypeScript + +```ts +function makeLargestSpecial(s: string): string { + if (s.length === 0) { + return ''; + } + + const ans: string[] = []; + let cnt = 0; + + for (let i = 0, j = 0; i < s.length; ++i) { + cnt += s[i] === '1' ? 1 : -1; + if (cnt === 0) { + const t = '1' + makeLargestSpecial(s.substring(j + 1, i)) + '0'; + ans.push(t); + j = i + 1; + } + } + + ans.sort((a, b) => b.localeCompare(a)); + return ans.join(''); +} +``` + diff --git a/solution/0700-0799/0761.Special Binary String/Solution.cpp b/solution/0700-0799/0761.Special Binary String/Solution.cpp index a1566ee7e8236..09392391c0f54 100644 --- a/solution/0700-0799/0761.Special Binary String/Solution.cpp +++ b/solution/0700-0799/0761.Special Binary String/Solution.cpp @@ -1,7 +1,9 @@ class Solution { public: string makeLargestSpecial(string s) { - if (s == "") return s; + if (s == "") { + return s; + } vector ans; int cnt = 0; for (int i = 0, j = 0; i < s.size(); ++i) { diff --git a/solution/0700-0799/0761.Special Binary String/Solution.ts b/solution/0700-0799/0761.Special Binary String/Solution.ts new file mode 100644 index 0000000000000..c9b73d095ea85 --- /dev/null +++ b/solution/0700-0799/0761.Special Binary String/Solution.ts @@ -0,0 +1,20 @@ +function makeLargestSpecial(s: string): string { + if (s.length === 0) { + return ''; + } + + const ans: string[] = []; + let cnt = 0; + + for (let i = 0, j = 0; i < s.length; ++i) { + cnt += s[i] === '1' ? 1 : -1; + if (cnt === 0) { + const t = '1' + makeLargestSpecial(s.substring(j + 1, i)) + '0'; + ans.push(t); + j = i + 1; + } + } + + ans.sort((a, b) => b.localeCompare(a)); + return ans.join(''); +}