Longest Substring Without Repeating Characters
Example
Note
Code
public class Solution {
/**
* @param s: a string
* @return: an integer
*/
public int lengthOfLongestSubstring(String s) {
// write your code here
int[] set = new int[256];
int res = 0, len = s.length();
int j = 0;
for (int i = 0; i < len; i++) {
while (j < len && set[s.charAt(j)] == 0) {
set[s.charAt(j)]++;
j++;
}
res = Math.max(res, j - i);
set[s.charAt(i)] = 0;
}
return res;
}
}Last updated