code

 avatar
unknown
plain_text
3 months ago
887 B
6
Indexable
class Solution {
    public int longestUniqueSubstr(String s) {
        // code here
        
        int left = 0;
        int right = 1;
        int res = 1;
        
        final boolean[] ispresent = new boolean[26];
        
        while (left <= right && right < s.length()) {
            ispresent[s.charAt(left) - 'a'] = true;
            
            if (left == right) {
                right++;
                continue;
            }
            
            while (right < s.length() && !ispresent[s.charAt(right) - 'a']) {
                ispresent[s.charAt(right) - 'a'] = true;
                right++;
            }
            
            int new_substring = right - left;
            
            res = res < new_substring ? new_substring : res;
            ispresent[s.charAt(left) - 'a'] = false;
            left++;
        }
        
        return res;
    }
}
Editor is loading...
Leave a Comment