Skip to content

Commit ab26852

Browse files
committed
feat: add solutions to lc problem: No.0761
No.0761.Special Binary String
1 parent 17a6980 commit ab26852

File tree

7 files changed

+248
-3
lines changed

7 files changed

+248
-3
lines changed

solution/0700-0799/0761.Special Binary String/README.md

+92-1
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,113 @@
3838

3939
<!-- 这里可写通用的实现逻辑 -->
4040

41+
**方法一:递归 + 排序**
42+
43+
我们可以把特殊的二进制序列看作“有效的括号”,$1$ 代表左括号,$0$ 代表右括号。例如,"11011000" 可以看作:"(()(()))"。
44+
45+
交换两个连续非空的特殊子串,相当于交换两个相邻的有效括号,我们可以使用递归来解决这个问题。
46+
47+
我们将字符串 $s$ 中的每个“有效的括号”都看作一部分,递归处理,最后进行排序,得到最终答案。
48+
49+
时间复杂度 $O(n^2)$。
50+
4151
<!-- tabs:start -->
4252

4353
### **Python3**
4454

4555
<!-- 这里可写当前语言的特殊实现逻辑 -->
4656

4757
```python
48-
58+
class Solution:
59+
def makeLargestSpecial(self, s: str) -> str:
60+
if s == '':
61+
return ''
62+
ans = []
63+
cnt = 0
64+
i = j = 0
65+
while i < len(s):
66+
cnt += 1 if s[i] == '1' else -1
67+
if cnt == 0:
68+
ans.append('1' + self.makeLargestSpecial(s[j + 1: i]) + '0')
69+
j = i + 1
70+
i += 1
71+
ans.sort(reverse=True)
72+
return ''.join(ans)
4973
```
5074

5175
### **Java**
5276

5377
<!-- 这里可写当前语言的特殊实现逻辑 -->
5478

5579
```java
80+
class Solution {
81+
public String makeLargestSpecial(String s) {
82+
if ("".equals(s)) {
83+
return "";
84+
}
85+
List<String> ans = new ArrayList<>();
86+
int cnt = 0;
87+
for (int i = 0, j = 0; i < s.length(); ++i) {
88+
cnt += s.charAt(i) == '1' ? 1 : -1;
89+
if (cnt == 0) {
90+
String t = "1" + makeLargestSpecial(s.substring(j + 1, i)) + "0";
91+
ans.add(t);
92+
j = i + 1;
93+
}
94+
}
95+
ans.sort(Comparator.reverseOrder());
96+
return String.join("", ans);
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
string makeLargestSpecial(string s) {
107+
if (s == "") return s;
108+
vector<string> ans;
109+
int cnt = 0;
110+
for (int i = 0, j = 0; i < s.size(); ++i)
111+
{
112+
cnt += s[i] == '1' ? 1 : -1;
113+
if (cnt == 0)
114+
{
115+
ans.push_back("1" + makeLargestSpecial(s.substr(j + 1, i - j - 1)) + "0");
116+
j = i + 1;
117+
}
118+
}
119+
sort(ans.begin(), ans.end(), greater<string>{});
120+
return accumulate(ans.begin(), ans.end(), ""s);
121+
}
122+
};
123+
```
56124
125+
### **Go**
126+
127+
```go
128+
func makeLargestSpecial(s string) string {
129+
if s == "" {
130+
return ""
131+
}
132+
ans := sort.StringSlice{}
133+
cnt := 0
134+
for i, j := 0, 0; i < len(s); i++ {
135+
if s[i] == '1' {
136+
cnt++
137+
} else {
138+
cnt--
139+
}
140+
if cnt == 0 {
141+
ans = append(ans, "1"+makeLargestSpecial(s[j+1:i])+"0")
142+
j = i + 1
143+
}
144+
}
145+
sort.Sort(sort.Reverse(ans))
146+
return strings.Join(ans, "")
147+
}
57148
```
58149

59150
### **...**

solution/0700-0799/0761.Special Binary String/README_EN.md

+82-1
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,94 @@ This is the lexicographically largest string possible after some number of swaps
5050
### **Python3**
5151

5252
```python
53-
53+
class Solution:
54+
def makeLargestSpecial(self, s: str) -> str:
55+
if s == '':
56+
return ''
57+
ans = []
58+
cnt = 0
59+
i = j = 0
60+
while i < len(s):
61+
cnt += 1 if s[i] == '1' else -1
62+
if cnt == 0:
63+
ans.append('1' + self.makeLargestSpecial(s[j + 1: i]) + '0')
64+
j = i + 1
65+
i += 1
66+
ans.sort(reverse=True)
67+
return ''.join(ans)
5468
```
5569

5670
### **Java**
5771

5872
```java
73+
class Solution {
74+
public String makeLargestSpecial(String s) {
75+
if ("".equals(s)) {
76+
return "";
77+
}
78+
List<String> ans = new ArrayList<>();
79+
int cnt = 0;
80+
for (int i = 0, j = 0; i < s.length(); ++i) {
81+
cnt += s.charAt(i) == '1' ? 1 : -1;
82+
if (cnt == 0) {
83+
String t = "1" + makeLargestSpecial(s.substring(j + 1, i)) + "0";
84+
ans.add(t);
85+
j = i + 1;
86+
}
87+
}
88+
ans.sort(Comparator.reverseOrder());
89+
return String.join("", ans);
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
string makeLargestSpecial(string s) {
100+
if (s == "") return s;
101+
vector<string> ans;
102+
int cnt = 0;
103+
for (int i = 0, j = 0; i < s.size(); ++i)
104+
{
105+
cnt += s[i] == '1' ? 1 : -1;
106+
if (cnt == 0)
107+
{
108+
ans.push_back("1" + makeLargestSpecial(s.substr(j + 1, i - j - 1)) + "0");
109+
j = i + 1;
110+
}
111+
}
112+
sort(ans.begin(), ans.end(), greater<string>{});
113+
return accumulate(ans.begin(), ans.end(), ""s);
114+
}
115+
};
116+
```
59117
118+
### **Go**
119+
120+
```go
121+
func makeLargestSpecial(s string) string {
122+
if s == "" {
123+
return ""
124+
}
125+
ans := sort.StringSlice{}
126+
cnt := 0
127+
for i, j := 0, 0; i < len(s); i++ {
128+
if s[i] == '1' {
129+
cnt++
130+
} else {
131+
cnt--
132+
}
133+
if cnt == 0 {
134+
ans = append(ans, "1"+makeLargestSpecial(s[j+1:i])+"0")
135+
j = i + 1
136+
}
137+
}
138+
sort.Sort(sort.Reverse(ans))
139+
return strings.Join(ans, "")
140+
}
60141
```
61142

62143
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
string makeLargestSpecial(string s) {
4+
if (s == "") return s;
5+
vector<string> ans;
6+
int cnt = 0;
7+
for (int i = 0, j = 0; i < s.size(); ++i)
8+
{
9+
cnt += s[i] == '1' ? 1 : -1;
10+
if (cnt == 0)
11+
{
12+
ans.push_back("1" + makeLargestSpecial(s.substr(j + 1, i - j - 1)) + "0");
13+
j = i + 1;
14+
}
15+
}
16+
sort(ans.begin(), ans.end(), greater<string>{});
17+
return accumulate(ans.begin(), ans.end(), ""s);
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func makeLargestSpecial(s string) string {
2+
if s == "" {
3+
return ""
4+
}
5+
ans := sort.StringSlice{}
6+
cnt := 0
7+
for i, j := 0, 0; i < len(s); i++ {
8+
if s[i] == '1' {
9+
cnt++
10+
} else {
11+
cnt--
12+
}
13+
if cnt == 0 {
14+
ans = append(ans, "1"+makeLargestSpecial(s[j+1:i])+"0")
15+
j = i + 1
16+
}
17+
}
18+
sort.Sort(sort.Reverse(ans))
19+
return strings.Join(ans, "")
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public String makeLargestSpecial(String s) {
3+
if ("".equals(s)) {
4+
return "";
5+
}
6+
List<String> ans = new ArrayList<>();
7+
int cnt = 0;
8+
for (int i = 0, j = 0; i < s.length(); ++i) {
9+
cnt += s.charAt(i) == '1' ? 1 : -1;
10+
if (cnt == 0) {
11+
String t = "1" + makeLargestSpecial(s.substring(j + 1, i)) + "0";
12+
ans.add(t);
13+
j = i + 1;
14+
}
15+
}
16+
ans.sort(Comparator.reverseOrder());
17+
return String.join("", ans);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def makeLargestSpecial(self, s: str) -> str:
3+
if s == '':
4+
return ''
5+
ans = []
6+
cnt = 0
7+
i = j = 0
8+
while i < len(s):
9+
cnt += 1 if s[i] == '1' else -1
10+
if cnt == 0:
11+
ans.append('1' + self.makeLargestSpecial(s[j + 1: i]) + '0')
12+
j = i + 1
13+
i += 1
14+
ans.sort(reverse=True)
15+
return ''.join(ans)

solution/1800-1899/1881.Maximum Value after Insertion/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ def maxValue(self, n: str, x: int) -> str:
88
else:
99
for i, c in enumerate(n[1:]):
1010
if int(c) > x:
11-
return n[:i + 1] + str(x) + n[i + 1:]
11+
return n[: i + 1] + str(x) + n[i + 1 :]
1212
return n + str(x)

0 commit comments

Comments
 (0)