LeetCode Note Java 00424:Longest Repeating Character Replacement
給定 k
次替換字母機會,回傳字串 s
經過替換後可形成的最長重複字母字串的長度。
題目
Longest Repeating Character Replacement Medium
You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times.
Return the length of the longest substring containing the same letter you can get after performing the above operations.
解法
列出想法:
- 題目換句話說:一定範圍內,非重複最多次的字母數量小於等於 k
- 判斷新範圍合不合理的方法
- 雙指針走完一條 String
- 指針前進條件:
- 左指針:不符合維持範圍條件,所以前進左指針
- 右指針:符合維持範圍條件,所以前進右指針
1 | class Solution { |
參考資料
leetcode/java/0424-longest-repeating-character-replacement.java
【LeetCode】424. 替换后的最长重复字符 Longest Repeating Character Replacement(Python)