Untitled
unknown
plain_text
3 years ago
1.1 kB
9
Indexable
class Solution {
public int lengthOfLongestSubstring(String s) {
HashMap<Character, Integer> mp = new HashMap<>();
int inc = -1;
int exc = -1;
int ans = 0;
while (true) {
boolean f1 = false;
while (inc < s.length() - 1) {
inc++;
f1 = true;
char ch = s.charAt(inc);
mp.put(ch, mp.getOrDefault(ch, 0) + 1);
if (mp.get(ch) > 1) {
break;
}
int len = inc - exc;
if (len > ans) {
ans = len;
}
}
boolean f2 = false;
while (exc < inc) {
exc++;
f2 = true;
char ch = s.charAt(exc);
mp.put(ch, mp.get(ch) - 1);
if (mp.get(ch) == 1) {
break;
}
}
if (!f1 && !f2) {
break;
}
}
return ans;
}
}
Editor is loading...