The solution beats 96.90% of cpp coders


  • 0
    B
    class Solution 
    {
    public:
    int lengthOfLongestSubstring(string s) 
    {
        int size=s.size();
        
        int table[256];
        for(int i=0;i<256;i++)
        {
            table[i]=-1;
        }
        
        int ret=0;
        int currtL=0;
        int currtR=size-1;
        for(int i=0;i<size;i++)
        {
            if(table[s[i]]!=-1)
            {
                int preIdx=table[s[i]];
                if(preIdx>=currtL)
                {
                    int tmp=i-currtL;
                    if(tmp>ret)
                    {
                        ret=tmp;
                    }
                    currtL=preIdx+1;
                    if(size-currtL<=ret){break;}
                }
            }
            table[s[i]]=i;
        }
        if(ret<currtR-currtL+1)
        {
          ret=currtR-currtL+1;
        }
        return ret;
    }
    

    };


Log in to reply
 

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