-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLongestUniqueSubstring.java
63 lines (60 loc) · 1.99 KB
/
LongestUniqueSubstring.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*https://practice.geeksforgeeks.org/problems/length-of-the-longest-substring3036/1*/
//sliding window and hashing
class Solution
{
int longestUniqueSubsttr(String S)
{
int i = 0, j = -1, n = S.length();
int result = Integer.MIN_VALUE;
int[] hash = new int[26];
int lastIndex = -1;
while (i < n && j < n-1) //till we reach the end
{
++j; //increase the window size
++hash[S.charAt(j)-'a']; //add it to hash
lastIndex = j; //store the last added index
/*
If the latest count is 1
update the substring length
*/
if (hash[S.charAt(lastIndex)-'a'] == 1)
result = Math.max(result,j-i+1);
/*
Till the lastest added character that is repeating
reduce window size from left
*/
while (hash[S.charAt(lastIndex)-'a'] > 1)
--hash[S.charAt(i++)-'a'];
}
return result;
}
}
/*https://leetcode.com/problems/longest-substring-without-repeating-characters/*/
//leetcode version
class Solution {
public int lengthOfLongestSubstring(String s) {
int i = 0, j = -1, n = s.length();
int result = Integer.MIN_VALUE;
int[] hash = new int[256];
int lastIndex = -1;
while (i < n && j < n-1) //till we reach the end
{
++j; //increase the window size
++hash[s.charAt(j)]; //add it to hash
lastIndex = j; //store the last added index
/*
If the latest count is 1
update the substring length
*/
if (hash[s.charAt(lastIndex)] == 1)
result = Math.max(result,j-i+1);
/*
Till the lastest added character that is repeating
reduce window size from left
*/
while (hash[s.charAt(lastIndex)] > 1)
--hash[s.charAt(i++)];
}
return result == Integer.MIN_VALUE ? 0 : result;
}
}