Merge pull request #1475 from sexyxlyWAol/master

Update 1221.分割平衡字符串.md (added python and go solution to 1221)
This commit is contained in:
程序员Carl
2022-07-25 09:08:48 +08:00
committed by GitHub

View File

@ -108,11 +108,38 @@ class Solution {
### Python
```python
class Solution:
def balancedStringSplit(self, s: str) -> int:
diff = 0 #右左差值
ans = 0
for c in s:
if c == "L":
diff -= 1
else:
diff += 1
if tilt == 0:
ans += 1
return ans
```
### Go
```go
func balancedStringSplit(s string) int {
diff := 0 // 右左差值
ans := 0
for _, c := range s {
if c == 'L' {
diff--
}else {
diff++
}
if diff == 0 {
ans++
}
}
return ans
}
```
### JavaScript