3. Longest Substring Without Repeating Characters
Last updated
Last updated
class Solution {
public int lengthOfLongestSubstring(String s) {
int res = 0;
int[] dp = new int[s.length() + 1];
for (int i = 1; i <= s.length(); i++) {
int realIndex = i - 1;
int index = s.substring(realIndex - dp[i - 1], realIndex).lastIndexOf(s.charAt(realIndex));
if (index == -1) dp[i] = dp[i - 1] + 1;
else dp[i] = dp[i - 1] - index;
res = Math.max(res, dp[i]);
}
return res;
}
}