C# in O(n)


  • 0
    A
    public int LengthOfLongestSubstring(string s)
    {
        char c = '\0';
        int longest = 0, start = 0, end = 0;
        int[] map = new int[128];
        for (int m = 0; m < map.Length; ++m)
            map[m] = -1;
    
        for (end = 0; end < s.Length; ++end)
        {
            c = s[end];
            start = Math.Max(start, map[c] + 1);
            map[c] = end;
            longest = Math.Max(longest, end - start + 1);
        }
    
        return longest;
    }
    

Log in to reply
 

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