My java solution with 412 ms


  • 1
    Z
    public class Solution {
        public int lengthOfLongestSubstring(String s) {
        int[] m = new int[127];
        char[] ch = s.toCharArray();
        int max = 0;
        int start = 0;
        for (int i = 0; i < m.length; i++) {
            m[i] = -1;
        }
        for (int i = 0; i < ch.length; i++) {
            if (m[ch[i]] < 0) {
                m[ch[i]] = i;
            } else {
                start = Math.max(start, m[ch[i]] + 1);
                m[ch[i]] = i;
            }
            max = Math.max(max, i - start + 1);
        }
        return max;
    }

Log in to reply
 

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