Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions solution/0700-0799/0761.Special Binary String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ tags:

### 方法一:递归 + 排序

我们可以把特殊的二进制序列看作“有效的括号”,$1$ 代表左括号,$0$ 代表右括号。例如,"11011000" 可以看作:"(()(()))"。
我们可以把特殊的二进制序列看作“有效的括号”,其中 $1$ 代表左括号,$0$ 代表右括号。例如,"11011000" 可以看作:"(()(()))"。

交换两个连续非空的特殊子串,相当于交换两个相邻的有效括号,我们可以使用递归来解决这个问题。

我们将字符串 $s$ 中的每个“有效的括号”都看作一部分,递归处理,最后进行排序,得到最终答案。

时间复杂度 $O(n^2)$。
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度

<!-- tabs:start -->

Expand Down Expand Up @@ -113,7 +113,9 @@ class Solution {
class Solution {
public:
string makeLargestSpecial(string s) {
if (s == "") return s;
if (s == "") {
return s;
}
vector<string> ans;
int cnt = 0;
for (int i = 0, j = 0; i < s.size(); ++i) {
Expand Down Expand Up @@ -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('');
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
39 changes: 37 additions & 2 deletions solution/0700-0799/0761.Special Binary String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ This is the lexicographically largest string possible after some number of swaps

<!-- solution:start -->

### 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$.

<!-- tabs:start -->

Expand Down Expand Up @@ -116,7 +124,9 @@ class Solution {
class Solution {
public:
string makeLargestSpecial(string s) {
if (s == "") return s;
if (s == "") {
return s;
}
vector<string> ans;
int cnt = 0;
for (int i = 0, j = 0; i < s.size(); ++i) {
Expand Down Expand Up @@ -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('');
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
4 changes: 3 additions & 1 deletion solution/0700-0799/0761.Special Binary String/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class Solution {
public:
string makeLargestSpecial(string s) {
if (s == "") return s;
if (s == "") {
return s;
}
vector<string> ans;
int cnt = 0;
for (int i = 0, j = 0; i < s.size(); ++i) {
Expand Down
20 changes: 20 additions & 0 deletions solution/0700-0799/0761.Special Binary String/Solution.ts
Original file line number Diff line number Diff line change
@@ -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('');
}