Can someone tell me what's the problem in my code!!!!!


  • 0
    C

    class Solution {
    public:
    int lengthOfLongestSubstring(string s) {
    unordered_map<char,int> hmap;
    int s_size =0,local_max =0,max = 0;
    int pre_size =0,size=0;
    for(int i = 0; i < s.size(); i++)
    {
    if(s.empty())
    return 0;
    else{
    if(hmap.count(s[i])==0)//new element
    {
    local_max++;
    hmap[s[i]] = 1;
    }
    else if (hmap.count(s[i]) != 0)//dupulicate which mean we need erase the part before the dupulicate element in hmap
    {
    pre_size = hmap.size();
    hmap.erase(hmap.find(s[i]),hmap.end());
    size = hmap.size();
    //cout << "pre_size = " << pre_size << " size = " << size <<endl;
    local_max = size+1;
    hmap[s[i]]=1;
    }
    else{
    local_max++;
    hmap[s[i]] =1;
    }
    if(local_max > max)
    max = local_max;
    }
    }

    return max;
    
    }
    

    };


Log in to reply
 

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