Concise C++ Solution 3ms

  • 0

    Check out another 3ms C++ solution. And it looks like we only need to add code to move the pointer further to boost the runtime to 3ms.

    while(j<s.size()&&total[s[j]-'a']<k) j++;


    class Solution {
        int longestSubstring(string s, int k) {
            int total[26]={0};
            for(int i=0;i<s.size();i++){
            int i=0;
            while(i<s.size()&&total[s[i]-'a']>=k) i++;
            if(i==s.size()) return i;
            int j=i;
            while(j<s.size()&&total[s[j]-'a']<k) j++;
            return max(longestSubstring(s.substr(0,i),k), longestSubstring(s.substr(j),k));

Log in to reply

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