9 ms 9 lines C++ solution


  • 0
    W
    // Inspired by @lightmark and a little faster than that one
    static int x = [](){
        std::ios::sync_with_stdio(false);
        cin.tie(NULL);
        return 0;
    }();
    
    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            int* hash = new int[128]();
            int longest = 0, i = 0, j = 0;
            for (i; i < 128; ++i) hash[i] = -1;
            for (i = 0, j; j < s.size(); ++j) {
                i = max(hash[s[j]]+1, i);
                hash[s[j]] = j;
                longest = max(longest, (j-i+1));
            }
            return longest;
        }
    };
    

Log in to reply
 

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