|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | import java.util.HashMap;
|
| 4 | +import java.util.HashSet; |
4 | 5 | import java.util.Map;
|
| 6 | +import java.util.Set; |
5 | 7 |
|
6 |
| -/**Given a string, find the length of the longest substring without repeating characters. |
| 8 | +/** |
| 9 | + * 3. Longest Substring Without Repeating Characters |
| 10 | + * Given a string, find the length of the longest substring without repeating characters. |
7 | 11 |
|
8 | 12 | Examples:
|
9 | 13 |
|
10 | 14 | Given "abcabcbb", the answer is "abc", which the length is 3.
|
11 | 15 |
|
12 | 16 | Given "bbbbb", the answer is "b", with the length of 1.
|
13 | 17 |
|
14 |
| - Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.*/ |
| 18 | + Given "pwwkew", the answer is "wke", with the length of 3. |
| 19 | +
|
| 20 | + Note that the answer must be a substring, "pwke" is a subsequence and not a substring. |
| 21 | + */ |
| 22 | + |
15 | 23 | public class _3 {
|
16 | 24 |
|
17 |
| - public static int lengthOfLongestSubstring(String s) { |
18 |
| - int result = 0; |
19 |
| - Map<Character, Integer> map = new HashMap(); |
20 |
| - for (int i = 0, j = i; j < s.length(); ){ |
21 |
| - if (!map.containsKey(s.charAt(j)) || (map.containsKey(s.charAt(j)) && map.get(s.charAt(j)) == 0)){ |
22 |
| - map.put(s.charAt(j), 1); |
23 |
| - result = Math.max(j-i+1, result); |
24 |
| - j++; |
25 |
| - } else { |
26 |
| - map.put(s.charAt(i), map.get(s.charAt(i)) - 1); |
27 |
| - i++; |
| 25 | + public static class Solution1 { |
| 26 | + public int lengthOfLongestSubstring(String s) { |
| 27 | + int result = 0; |
| 28 | + Map<Character, Integer> map = new HashMap(); |
| 29 | + for (int i = 0, j = i; j < s.length(); ) { |
| 30 | + if (!map.containsKey(s.charAt(j)) || (map.containsKey(s.charAt(j)) && map.get(s.charAt(j)) == 0)) { |
| 31 | + map.put(s.charAt(j), 1); |
| 32 | + result = Math.max(j - i + 1, result); |
| 33 | + j++; |
| 34 | + } else { |
| 35 | + map.put(s.charAt(i), map.get(s.charAt(i)) - 1); |
| 36 | + i++; |
| 37 | + } |
| 38 | + } |
| 39 | + return result; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public static class Solution2 { |
| 44 | + /** |
| 45 | + * Sliding Window |
| 46 | + * O(n) time |
| 47 | + * O(min(m,n)) or O(k) space |
| 48 | + */ |
| 49 | + public int lengthOfLongestSubstring(String s) { |
| 50 | + int n = s.length(); |
| 51 | + Set<Character> set = new HashSet<>(); |
| 52 | + int result = 0; |
| 53 | + int i = 0; |
| 54 | + int j = 0; |
| 55 | + while (i < n && j < n) { |
| 56 | + /**Try to extend the range i, j*/ |
| 57 | + if (!set.contains(s.charAt(j))) { |
| 58 | + set.add(s.charAt(j++)); |
| 59 | + result = Math.max(result, j - i); |
| 60 | + } else { |
| 61 | + set.remove(s.charAt(i++)); |
| 62 | + } |
| 63 | + } |
| 64 | + return result; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public static class Solution3 { |
| 69 | + /**Sliding Window |
| 70 | + * O(n) time |
| 71 | + * O(n) space |
| 72 | + */ |
| 73 | + public int lengthOfLongestSubstring(String s) { |
| 74 | + if (s.length() == 0) { |
| 75 | + return 0; |
| 76 | + } |
| 77 | + int max = 0; |
| 78 | + Map<Character, Integer> map = new HashMap<>(); |
| 79 | + /**Try to extend the range (i, j)*/ |
| 80 | + for (int i = 0, j = 0; i < s.length(); i++) { |
| 81 | + if (map.containsKey(s.charAt(i))) { |
| 82 | + j = Math.max(j, map.get(s.charAt(i)) + 1); |
| 83 | + } |
| 84 | + map.put(s.charAt(i), i); |
| 85 | + max = Math.max(max, i + 1 - j); |
28 | 86 | }
|
| 87 | + return max; |
29 | 88 | }
|
30 |
| - return result; |
31 | 89 | }
|
32 | 90 |
|
33 |
| - public static void main(String...args){ |
34 |
| -// String s = "abcabcbb"; |
35 |
| - String s = "pwwkew"; |
36 |
| - System.out.println(lengthOfLongestSubstring(s)); |
| 91 | + public static class Solution4 { |
| 92 | + /**Sliding Window Optimized |
| 93 | + * O(n) time |
| 94 | + * O(n) space |
| 95 | + */ |
| 96 | + public int lengthOfLongestSubstring(String s) { |
| 97 | + if (s.length() == 0) { |
| 98 | + return 0; |
| 99 | + } |
| 100 | + int max = 0; |
| 101 | + int[] index = new int[128]; |
| 102 | + /**Try to extend the range (i, j)*/ |
| 103 | + for (int i = 0, j = 0; j < s.length(); j++) { |
| 104 | + i = Math.max(index[s.charAt(j)], i); |
| 105 | + max = Math.max(max, j -i + 1); |
| 106 | + index[s.charAt(j)] = j+1; |
| 107 | + } |
| 108 | + return max; |
| 109 | + } |
37 | 110 | }
|
38 | 111 | }
|
0 commit comments