-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSP.java
30 lines (22 loc) · 912 Bytes
/
LSP.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
class LSP {
public int lengthOfLongestSubstring(String s) {
int[] index = new int[128]; // ASCII table size
int maxLength = 0;
int i = 0; // Left boundary of the sliding window
for (int j = 0; j < s.length(); j++) {
char currentChar = s.charAt(j);
// Update i to the max of its current position and the last seen position of
// currentChar
i = Math.max(index[currentChar], i);
// Calculate the length of the current window
maxLength = Math.max(maxLength, j - i + 1);
// Update the index of the current character to the next position (j + 1)
index[currentChar] = j + 1;
}
return maxLength;
}
public static void main(String[] args) {
LSP lps = new LSP();
System.out.println(lps.lengthOfLongestSubstring("pwwkew")); // Output:
}
}