My solution in 64 ms


  • 0
    X
    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            const char *hash[256] = {};
            const char *str = s.c_str();
            const char *head = str;
            int max_len = 0;
            while (*str) {
                    if (hash[*str] >= head) {
                            if (str - head > max_len)
                                    max_len = str - head;
    
                            head = hash[*str] + 1;
                    }
                    hash[*str] = str;
                    ++str;
            }
    
            if (str - head > max_len)
                    max_len = str - head;
    
            return max_len;
        }
    };

Log in to reply
 

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