159:至多包含两个不同字符的最长子串
This commit is contained in:
parent
145a8f35e6
commit
75a1cf370d
@ -0,0 +1,59 @@
|
|||||||
|
//给定一个字符串 s ,找出 至多 包含两个不同字符的最长子串 t ,并返回该子串的长度。
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
// 输入: "eceba"
|
||||||
|
//输出: 3
|
||||||
|
//解释: t 是 "ece",长度为3。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
// 输入: "ccaabbb"
|
||||||
|
//输出: 5
|
||||||
|
//解释: t 是 "aabbb",长度为5。
|
||||||
|
//
|
||||||
|
// Related Topics 哈希表 字符串 滑动窗口 👍 132 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
//159:至多包含两个不同字符的最长子串
|
||||||
|
class LongestSubstringWithAtMostTwoDistinctCharacters {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new LongestSubstringWithAtMostTwoDistinctCharacters().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int lengthOfLongestSubstringTwoDistinct(String s) {
|
||||||
|
int n = s.length();
|
||||||
|
if (n < 3) return n;
|
||||||
|
int left = 0;
|
||||||
|
int right = 0;
|
||||||
|
Map<Character, Integer> hashmap = new HashMap<>();
|
||||||
|
|
||||||
|
int max_len = 2;
|
||||||
|
|
||||||
|
while (right < n) {
|
||||||
|
if (hashmap.size() < 3)
|
||||||
|
hashmap.put(s.charAt(right), right++);
|
||||||
|
if (hashmap.size() == 3) {
|
||||||
|
int del_idx = Collections.min(hashmap.values());
|
||||||
|
hashmap.remove(s.charAt(del_idx));
|
||||||
|
// move left pointer of the slidewindow
|
||||||
|
left = del_idx + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
max_len = Math.max(max_len, right - left);
|
||||||
|
}
|
||||||
|
return max_len;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
<p>给定一个字符串<strong><em> s</em></strong> ,找出 <strong>至多 </strong>包含两个不同字符的最长子串 <strong><em>t</em> </strong>,并返回该子串的长度。</p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong> "eceba"
|
||||||
|
<strong>输出: </strong>3
|
||||||
|
<strong>解释: <em>t</em></strong> 是 "ece",长度为3。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong> "ccaabbb"
|
||||||
|
<strong>输出: </strong>5
|
||||||
|
<strong>解释: <em>t</em></strong><em> </em>是 "aabbb",长度为5。
|
||||||
|
</pre>
|
||||||
|
<div><div>Related Topics</div><div><li>哈希表</li><li>字符串</li><li>滑动窗口</li></div></div><br><div><li>👍 132</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user