topic:
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 | class Solution: |
1 | func balancedString(s string) int { |