11---
22title : 1234. Replace the sub -string to get a balanced string One question daily
3- date : ' 2024.01.01 0:00'
3+ date : " 2024.01.01 0:00"
44tags :
55 - - Python
66 - - answer
77abbrlink : 56d97dcf
8+ docId : p8igr19xfxnuyo2lpngnr6fg
89---
10+
911# topic:
1012
1113![ 2023-02-13.png] ( ..%2Fassets%2Fimg%2F2023-02-13.png )
1214[ 1234. Replace the sub -string to get a balanced string.md] ( https://leetcode.cn/problems/replace-the-substring-for-balanced-string/description/ )
1315
1416# Thought:
15- ` all() ` : if bool(x) For all the values in iterative objects x All for True,Then return True。 if可迭代对象为空,Then return True。
1617
18+ ` all() ` : if bool(x) For all the values in iterative objects x All for True,Then return True。 if可迭代对象为空,Then return True。
1719
1820Tongxiang dual pointer,The solution of the spiritual god of this question。
1921If in this string,These four characters happen just to appear n/4 Second-rate,Then it is one「Balanced string」。
2022if在待替换子串之外的任意字符的出现Second-rate数都** Exceed** $m=\dfrac{n}{4}$
2123,So no matter how you replace it,都无法make这个字符的出现Second-rate数等于m。
2224on the other hand,if在待替换子串之外的任意字符的出现Second-rate数都** 不Exceed** m,
23- > So it can be replaced ,make s 为Balanced string,即每个字符的出现Second-rate数均为 m。
24- For this question,The left and right end points of the sub -string are left and right,enumerate right,
25- if子串外的任意字符的出现Second-rate数都不Exceedm,The explanation from left arrive
26- rightThis sub -string can be to replace the sub -string,Length right−left+1
27- Update the minimum value of the answer,Move to the right left,Sumid sub -string length。
2825
26+ > So it can be replaced ,make s 为Balanced string,即每个字符的出现Second-rate数均为 m。
27+ > For this question,The left and right end points of the sub -string are left and right,enumerate right,
28+ > if子串外的任意字符的出现Second-rate数都不Exceedm,The explanation from left arrive
29+ > rightThis sub -string can be to replace the sub -string,Length right−left+1
30+ > Update the minimum value of the answer,Move to the right left,Sumid sub -string length。
2931
3032# Code:
3133
@@ -47,6 +49,7 @@ class Solution:
4749 return ans
4850
4951```
52+
5053``` golang
5154func balancedString (s string ) int {
5255 cnt , m := [' X' ]int {}, len (s)/4
0 commit comments