nord vpnnord vpn
Ad

Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
1.1 kB
2
Indexable
Never
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;
    }
}

nord vpnnord vpn
Ad