Simple Java solution


  • 1
        public int longestSubstring(String s, int k) {
            int[] arr = new int[26];
            for (char c : s.toCharArray()) {
                arr[c - 'a']++;
            }
            
            int idx = 0, max = 0;
            for (int i = 0; i < s.length(); i++) {
                if (arr[s.charAt(i) - 'a'] < k) {
                   max = Math.max(max, longestSubstring(s.substring(idx, i), k));
                   idx = i + 1;
                }
            }
            
            return idx == 0 ? s.length() : Math.max(max, longestSubstring(s.substring(idx), k));
        }
    

Log in to reply
 

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