My easy solution in JAVA (O(N)) .


  • 14
    I
    public class Solution {
        public int lengthOfLongestSubstring(String s) {
            int[] mOccur = new int[256];
            int maxL = 0;
            for(int i = 0, j = 0; i < s.length(); ++i){
                char ch = s.charAt(i);
                ++mOccur[ch];
                while(mOccur[ch] > 1){
                    --mOccur[s.charAt(j++)];
                }
                maxL = Math.max(maxL, i - j + 1);
            }
            return maxL;
        }
    }

  • 0
    Y

    this is an effictive and subtle solution


Log in to reply
 

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