avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

1234. Replace the sub -string to get a balanced string One question daily

  2024-01-01        
字数统计: 447字   |   阅读时长: 2min

topic:

2023-02-13.png
1234. Replace the sub -string to get a balanced string.md

Thought:

all():if bool(x) For all the values ​​in iterative objects x All for True,Then return True。 if可迭代对象为空,Then return True。

Tongxiang dual pointer,The solution of the spiritual god of this question。
If in this string,These four characters happen just to appear n/4 Second-rate,Then it is one「Balanced string」。
if在待替换子串之外的任意字符的出现Second-rate数都Exceed $m=\dfrac{n}{4}$
,So no matter how you replace it,都无法make这个字符的出现Second-rate数等于m。
on the other hand,if在待替换子串之外的任意字符的出现Second-rate数都不Exceed m,

So it can be replaced ,make s 为Balanced string,即每个字符的出现Second-rate数均为 m。
For this question,The left and right end points of the sub -string are left and right,enumerate right,
if子串外的任意字符的出现Second-rate数都不Exceedm,The explanation from left arrive
rightThis sub -string can be to replace the sub -string,Length right−left+1
Update the minimum value of the answer,Move to the right left,Sumid sub -string length。

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
def balancedString(self, s: str) -> int:
s_c = Counter(s)
n = len(s)
if all(s_c[v] <= n//4 for v in s_c):
return 0
ans, left = inf, 0
# enumerate右端点
for i, j in enumerate(s):
s_c[j] -= 1
while all(s_c[v] <= n // 4 for v in s_c):
ans = min(ans, i - left + 1)
s_c[s[left]] += 1
left += 1
return ans

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func balancedString(s string) int {
cnt, m := ['X']int{}, len(s)/4
for _, c := range s {
cnt[c]++
}
if cnt['Q'] == m && cnt['W'] == m && cnt['E'] == m && cnt['R'] == m {
return 0
}
ans, left := len(s), 0
for right, c := range s {
cnt[c]--
for cnt['Q'] <= m && cnt['W'] <= m && cnt['E'] <= m && cnt['R'] <= m {
ans = min(ans, right-left+1)
cnt[s[left]]++
left++
}
}
return ans
}
func min(a, b int) int { if a > b { return b }; return a }
  • Python
  • answer

扫一扫,分享到微信

微信分享二维码
1139. The greatest 1 Formation of the border One question daily
1233Delete the subfolder folder One question daily
目录
  1. 1. topic:
  2. 2. Thought:
  3. 3. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号