Skip to content

Commit 9b1793f

Browse files
committed
159 (1) two pointers window
1 parent aa5fdff commit 9b1793f

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
***************************************************************************
3+
* Description:
4+
*
5+
* Given a string, find the length of the longest substring T that contains
6+
* at most 2 distinct characters.
7+
*
8+
* For example, Given s = “eceba”, T is "ece" which its length is 3.
9+
*
10+
***************************************************************************
11+
* @tag : Hash Table; Two Pointers; String
12+
* {@link https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ }
13+
*/
14+
package _159_LongestSubstringWithAtMostTwoDistinctCharacters;
15+
16+
/** see test {@link _159_LongestSubstringWithAtMostTwoDistinctCharacters.PracticeTest } */
17+
public class Practice {
18+
19+
public int lengthOfLongestSubstringTwoDistinct(String s) {
20+
// TODO Auto-generated method stub
21+
return 0;
22+
}
23+
24+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Time : O() ; Space: O()
3+
* @tag : Hash Table; Two Pointers; String
4+
* @by : Steven Cooks
5+
* @date: Sep 30, 2015
6+
***************************************************************************
7+
* Description:
8+
*
9+
* Given a string, find the length of the longest substring T that contains
10+
* at most 2 distinct characters.
11+
*
12+
* For example, Given s = “eceba”, T is "ece" which its length is 3.
13+
*
14+
***************************************************************************
15+
* {@link https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ }
16+
*/
17+
package _159_LongestSubstringWithAtMostTwoDistinctCharacters;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
/** see test {@link _159_LongestSubstringWithAtMostTwoDistinctCharacters.SolutionTest } */
23+
public class Solution {
24+
25+
public int lengthOfLongestSubstringTwoDistinct(String s) {
26+
int res = 0;
27+
// char, first appearance after the other char in current window
28+
Map<Character, Integer> map = new HashMap<>();
29+
int start = 0;
30+
for (int i = 0; i < s.length(); i++) {
31+
char ch = s.charAt(i);
32+
if (map.size() < 2) {
33+
// less than two characters in current window
34+
if (!map.containsKey(ch)) {
35+
map.put(ch, i);
36+
}
37+
} else if (map.containsKey(ch)) {
38+
// duplicates appears, update index
39+
if (s.charAt(i - 1) != ch) {
40+
map.put(ch, i);
41+
}
42+
} else {
43+
// 3rd character appears
44+
start = map.get(s.charAt(i - 1));
45+
map = new HashMap<>();
46+
map.put(s.charAt(i - 1), start);
47+
map.put(ch, i);
48+
}
49+
// update global result
50+
if (i - start + 1 > res) {
51+
res = i - start + 1;
52+
}
53+
}
54+
return res;
55+
}
56+
57+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package _159_LongestSubstringWithAtMostTwoDistinctCharacters;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.rules.Timeout;
10+
11+
public class PracticeTest {
12+
13+
/** Test method for {@link _159_LongestSubstringWithAtMostTwoDistinctCharacters.Practice } */
14+
Practice solution;
15+
16+
@Rule
17+
public Timeout globalTimeout = new Timeout(200);
18+
19+
@Before
20+
public void setUp() throws Exception {
21+
solution = new Practice();
22+
}
23+
24+
@After
25+
public void tearDown() throws Exception {
26+
solution = null;
27+
}
28+
29+
@Test
30+
public void Test1() {
31+
String s = "eceba";
32+
int actual = solution.lengthOfLongestSubstringTwoDistinct(s);
33+
int expected = 3;
34+
assertEquals(expected, actual);
35+
}
36+
37+
@Test
38+
public void Test2() {
39+
String s = "aaaa";
40+
int actual = solution.lengthOfLongestSubstringTwoDistinct(s);
41+
int expected = 4;
42+
assertEquals(expected, actual);
43+
}
44+
45+
@Test
46+
public void Test3() {
47+
String s = "bbaabb";
48+
int actual = solution.lengthOfLongestSubstringTwoDistinct(s);
49+
int expected = 6;
50+
assertEquals(expected, actual);
51+
}
52+
53+
@Test
54+
public void Test4() {
55+
String s = "bbaabbc";
56+
int actual = solution.lengthOfLongestSubstringTwoDistinct(s);
57+
int expected = 6;
58+
assertEquals(expected, actual);
59+
}
60+
61+
@Test
62+
public void Test5() {
63+
String s = "bbacabbc";
64+
int actual = solution.lengthOfLongestSubstringTwoDistinct(s);
65+
int expected = 3;
66+
assertEquals(expected, actual);
67+
}
68+
69+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package _159_LongestSubstringWithAtMostTwoDistinctCharacters;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.rules.Timeout;
10+
11+
public class SolutionTest {
12+
13+
/** Test method for {@link _159_LongestSubstringWithAtMostTwoDistinctCharacters.Solution } */
14+
Solution solution;
15+
16+
@Rule
17+
public Timeout globalTimeout = new Timeout(200);
18+
19+
@Before
20+
public void setUp() throws Exception {
21+
solution = new Solution();
22+
}
23+
24+
@After
25+
public void tearDown() throws Exception {
26+
solution = null;
27+
}
28+
29+
@Test
30+
public void Test1() {
31+
String s = "eceba";
32+
int actual = solution.lengthOfLongestSubstringTwoDistinct(s);
33+
int expected = 3;
34+
assertEquals(expected, actual);
35+
}
36+
37+
@Test
38+
public void Test2() {
39+
String s = "aaaa";
40+
int actual = solution.lengthOfLongestSubstringTwoDistinct(s);
41+
int expected = 4;
42+
assertEquals(expected, actual);
43+
}
44+
45+
@Test
46+
public void Test3() {
47+
String s = "bbaabb";
48+
int actual = solution.lengthOfLongestSubstringTwoDistinct(s);
49+
int expected = 6;
50+
assertEquals(expected, actual);
51+
}
52+
53+
@Test
54+
public void Test4() {
55+
String s = "bbaabbc";
56+
int actual = solution.lengthOfLongestSubstringTwoDistinct(s);
57+
int expected = 6;
58+
assertEquals(expected, actual);
59+
}
60+
61+
@Test
62+
public void Test5() {
63+
String s = "bbacabbc";
64+
int actual = solution.lengthOfLongestSubstringTwoDistinct(s);
65+
int expected = 3;
66+
assertEquals(expected, actual);
67+
}
68+
69+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy