【LeetCode】 395. Longest Substring with At Least K Repeating Characters
来源:互联网 发布:澳门银河网络平台 编辑:程序博客网 时间:2024/06/12 10:13
Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times.
Example 1:
Input:s = "aaabb", k = 3Output:3The longest substring is "aaa", as 'a' is repeated 3 times.
Example 2:
Input:s = "ababbc", k = 2Output:5The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
public class Solution { public int longestSubstring(String s, int k) { char[] array = s.toCharArray(); return helper(array, 0, s.length(), k); } private int helper(char[] array, int start, int end, int k) { if (end - start < k) { return 0; } int[] count = new int[26]; for (int i = start; i < end; i++) { count[array[i] - 'a']++; } for (int i = 0; i < 26; i++) { if (count[i] < k && count[i] > 0) { for (int j = start; j < end; j++) { if (array[j] == i + 'a') { int left = helper(array, start, j, k); int right = helper(array, j + 1, end, k); return Math.max(left, right); } } } } return end - start; }}
0 0
- 【Leetcode】395. Longest Substring with At Least K Repeating Characters
- leetcode 395. Longest Substring with At Least K Repeating Characters
- [LeetCode] 395. Longest Substring with At Least K Repeating Characters
- LeetCode 395. Longest Substring with At Least K Repeating Characters
- 【LeetCode】 395. Longest Substring with At Least K Repeating Characters
- [LeetCode]395. Longest Substring with At Least K Repeating Characters
- 【Leetcode】395. Longest Substring with At Least K Repeating Characters
- Leetcode-395. Longest Substring with At Least K Repeating Characters
- [leetcode]395. Longest Substring with At Least K Repeating Characters
- Leetcode 395. Longest Substring with At Least K Repeating Characters
- [LeetCode]395. Longest Substring with At Least K Repeating Characters
- LeetCode[395] Longest Substring with At Least K Repeating Characters
- LeetCode 395 Longest Substring with At Least K Repeating Characters
- 395. Longest Substring with At Least K Repeating Characters
- 395. Longest Substring with At Least K Repeating Characters
- 395. Longest Substring with At Least K Repeating Characters
- 395. Longest Substring with At Least K Repeating Characters
- 395. Longest Substring with At Least K Repeating Characters
- 将Mule ESB Http项目转换为Tomcat项目(5) 改造Tomcat
- Linux开机自启动目录
- 简单编程题目连载(十一)——0-1背包问题
- Android主题与Toolbar样式之间的关系
- Activity的一些特点(1)
- 【LeetCode】 395. Longest Substring with At Least K Repeating Characters
- LeetCode 112. Path Sum
- 关于在不同版本和平台之间进行还原或复制的常见问题 (文档 ID 1526162.1)
- Linux Grub、Linux运行级别、终端模式登陆
- h5弹层之后阻止页面滚动
- 分布式框架学习
- [LeetCode]219 Contains Duplicate II
- UVa 11212 - Editing a Book
- Python爬虫包 BeautifulSoup 学习(九) 正则表达式与Lambda表达式