my simple solution


  • 0
    T

    public class Solution {

    public int LongestSubstring(string s, int k) {
        
        int len = s.Length;
        if(k <= 1)
            return len;
        
        int[] dp = new int[26];
        
        
        for(int i = 0;i<len;i++){
            dp[s[i]-'a']++;
        }
        
        if(!dp.Any(x=> x>0 && x< k)){
            return len;
        }
        
        int result = 0;
        int pIndex = 0;
        for(int i = 0;i<len;i++){
            if(dp[s[i]-'a']<k){
                string subStr = s.Substring(pIndex,i-pIndex);
                
                pIndex = i+1;
                if(subStr.Length <=1 )
                    continue;
                
                result = Math.Max(result,LongestSubstring(subStr,k));
            }
        }
        
        result = Math.Max(result,LongestSubstring(s.Substring(pIndex),k));
        
        return result;
    }
    

    }


Log in to reply
 

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