Time Limit Exceed Error


  • 0
    D

    My code is running fine locally but getting time limit exceed error.
    Can someone tell me where the extra time is taking and how can I optimize that?

    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            unordered_map<char, int> dict;
            int count = 0;
            int max = 0;
            int i = 0;
            while(i < s.size()){
                int j=i;
                while(j < s.size() && dict[s[j]] == 0) {
                    count++;
                    dict[s[j]] = j + 1;
                    j++;
                }
                if (count > max)    max = count;
                if(j == s.size()) break;
                count = 0;
                i = dict[s[j]];
                dict.clear();
            }
            return max;
        }
    };

Log in to reply
 

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