-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathSolution.java
42 lines (33 loc) · 1.08 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* @author Oleg Cherednik
* @since 27.01.2019
*/
public class Solution {
public static void main(String... args) {
System.out.println(findLongestSubstring("abcba", 2)); // bcb
}
public static String findLongestSubstring(String str, int k) {
Map<Character, Set<Integer>> map = new HashMap<>();
String res = null;
for (int i = 0, j = 0; j < str.length(); ) {
if (map.containsKey(str.charAt(j)))
map.get(str.charAt(j)).add(j++);
else if (map.size() < k) {
map.put(str.charAt(j), new HashSet<>());
map.get(str.charAt(j)).add(j++);
} else {
if (res == null || j - i + 1 > res.length())
res = str.substring(i, j);
map.get(str.charAt(i)).remove(i);
if (map.get(str.charAt(i)).isEmpty())
map.remove(str.charAt(i));
i++;
}
}
return res;
}
}