Skip to content

Commit 4c774a2

Browse files
committed
feat: add solutions to lc problem: No.1963
No.1963.Minimum Number of Swaps to Make the String Balanced
1 parent 1dda487 commit 4c774a2

File tree

6 files changed

+153
-2
lines changed

6 files changed

+153
-2
lines changed

solution/1900-1999/1963.Minimum Number of Swaps to Make the String Balanced/README.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,68 @@
7373
<!-- 这里可写当前语言的特殊实现逻辑 -->
7474

7575
```python
76-
76+
class Solution:
77+
def minSwaps(self, s: str) -> int:
78+
ans = 0
79+
for c in s:
80+
if c == '[':
81+
ans += 1
82+
elif ans:
83+
ans -= 1
84+
return (ans + 1) >> 1
7785
```
7886

7987
### **Java**
8088

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

8391
```java
92+
class Solution {
93+
public int minSwaps(String s) {
94+
int ans = 0;
95+
for (char c : s.toCharArray()) {
96+
if (c == '[') {
97+
++ans;
98+
} else if (ans > 0) {
99+
--ans;
100+
}
101+
}
102+
return (ans + 1) >> 1;
103+
}
104+
}
105+
```
106+
107+
### **C++**
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
int minSwaps(string s) {
113+
int ans = 0;
114+
for (char& c : s)
115+
{
116+
if (c == '[') ++ans;
117+
else if (ans) --ans;
118+
}
119+
return (ans + 1) >> 1;
120+
}
121+
};
122+
```
84123
124+
### **Go**
125+
126+
```go
127+
func minSwaps(s string) int {
128+
ans := 0
129+
for _, c := range s {
130+
if c == '[' {
131+
ans++
132+
} else if ans > 0 {
133+
ans--
134+
}
135+
}
136+
return (ans + 1) >> 1
137+
}
85138
```
86139

87140
### **...**

solution/1900-1999/1963.Minimum Number of Swaps to Make the String Balanced/README_EN.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,66 @@ The resulting string is &quot;[[][]]&quot;.
6565
### **Python3**
6666

6767
```python
68-
68+
class Solution:
69+
def minSwaps(self, s: str) -> int:
70+
ans = 0
71+
for c in s:
72+
if c == '[':
73+
ans += 1
74+
elif ans:
75+
ans -= 1
76+
return (ans + 1) >> 1
6977
```
7078

7179
### **Java**
7280

7381
```java
82+
class Solution {
83+
public int minSwaps(String s) {
84+
int ans = 0;
85+
for (char c : s.toCharArray()) {
86+
if (c == '[') {
87+
++ans;
88+
} else if (ans > 0) {
89+
--ans;
90+
}
91+
}
92+
return (ans + 1) >> 1;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
int minSwaps(string s) {
103+
int ans = 0;
104+
for (char& c : s)
105+
{
106+
if (c == '[') ++ans;
107+
else if (ans) --ans;
108+
}
109+
return (ans + 1) >> 1;
110+
}
111+
};
112+
```
74113
114+
### **Go**
115+
116+
```go
117+
func minSwaps(s string) int {
118+
ans := 0
119+
for _, c := range s {
120+
if c == '[' {
121+
ans++
122+
} else if ans > 0 {
123+
ans--
124+
}
125+
}
126+
return (ans + 1) >> 1
127+
}
75128
```
76129

77130
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int minSwaps(string s) {
4+
int ans = 0;
5+
for (char& c : s)
6+
{
7+
if (c == '[') ++ans;
8+
else if (ans) --ans;
9+
}
10+
return (ans + 1) >> 1;
11+
}
12+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func minSwaps(s string) int {
2+
ans := 0
3+
for _, c := range s {
4+
if c == '[' {
5+
ans++
6+
} else if ans > 0 {
7+
ans--
8+
}
9+
}
10+
return (ans + 1) >> 1
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int minSwaps(String s) {
3+
int ans = 0;
4+
for (char c : s.toCharArray()) {
5+
if (c == '[') {
6+
++ans;
7+
} else if (ans > 0) {
8+
--ans;
9+
}
10+
}
11+
return (ans + 1) >> 1;
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def minSwaps(self, s: str) -> int:
3+
ans = 0
4+
for c in s:
5+
if c == '[':
6+
ans += 1
7+
elif ans:
8+
ans -= 1
9+
return (ans + 1) >> 1

0 commit comments

Comments
 (0)