Beat 98% JAVA two pointer using slibling window


  • 0
    P
    public int lengthOfLongestSubstring(String s) {
          if (s == null || s.length() == 0) {
                return 0;
          }
          int i = 0, j = 0;
          int len = 1;
          int[] count = new int[128];
          while (j < s.length()) {
              if (count[s.charAt(j++)]++ == 0) continue; //no duplicate then we continue
              len = j - i - 1 > len? j - i - 1: len; // valid, update length
              while(count[s.charAt(i++)]-- == 1);// make a inValid
          }
          if (i < s.length()) return len < j - i? j - i: len; //corner case for "abcdefg" or "aab"
          return len;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.