Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d3dc6e7

Browse files
Longest Substring with At Most Two Distinct Characters
1 parent 27c6725 commit d3dc6e7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode2018;
2+
3+
import java.util.HashMap;
4+
5+
public class LongestSubstringMostkDistinctCharacters {
6+
7+
public int lengthOfLongestSubstringTwoDistinct(String s) {
8+
int k =2;
9+
HashMap<Character, Integer> map = new HashMap<>();
10+
int i =0;
11+
int max=0;
12+
for(int j = 0; j < s.length(); j++){
13+
map.put(s.charAt(j), j);
14+
if(map.size()>k){
15+
while(i<j && map.get(s.charAt(i))!=i) i++;
16+
map.remove(s.charAt(i++));
17+
}
18+
max = Math.max(max, j-i+1);
19+
}
20+
return max;
21+
}
22+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /