
问题
在一个由小写字母构成的字符串 S 中,包含由一些连续的相同字符所构成的分组。
例如,在字符串 S = “abbxxxxzyy” 中,就含有 “a”, “bb”, “xxxx”, “z” 和 “yy” 这样的一些分组。
我们称所有包含大于或等于三个连续字符的分组为较大分组。找到每一个较大分组的起始和终止位置。
最终结果按照字典顺序输出。
输入: “abbxxxxzzy”
输出: [[3,6]]
解释: “xxxx” 是一个起始于 3 且终止于 6 的较大分组。
输入: “abc”
输出: []
解释: “a”,”b” 和 “c” 均不是符合要求的较大分组。
输入: “abcdddeeeeaabbbcd”
输出: [[3,5],[6,9],[12,14]]
说明: 1 <= S.length <= 1000
In a string S of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like S = “abbxxxxzyy” has the groups “a”, “bb”, “xxxx”, “z” and “yy”.
Call a group large if it has 3 or more characters. We would like the starting and ending positions of every large group.
The final answer should be in lexicographic order.
Input: “abbxxxxzzy”
Output: [[3,6]]
Explanation: “xxxx” is the single large group with starting 3 and ending positions 6.
Input: “abc”
Output: []
Explanation: We have “a”,”b” and “c” but no large group.
Input: “abcdddeeeeaabbbcd”
Output: [[3,5],[6,9],[12,14]]
Note: 1 <= S.length <= 1000
示例
public class Program { public static void Main(string[] args) { string S = string.Empty; S = "abbxxxxzzy"; var res = LargeGroupPositions(S); ShowArray(res); S = "abcdddeeeeaabbbcd"; res = LargeGroupPositions2(S); ShowArray(res); Console.ReadKey(); } private static void ShowArray(IList<IList<int>> array) { foreach(var list in array) { foreach(var index in list) { Console.Write($"{index} "); } } Console.WriteLine(); } private static IList<IList<int>> LargeGroupPositions(string S) { var result = new List<IList<int>>(); var last = '\0'; var startIndex = -1; var endIndex = -1; for(var i = 0; i < S.Length; i++) { if(S[i] != last || i == S.Length - 1) { endIndex = i - 1; if(i == S.Length - 1 && S[i] == last) endIndex = i; if(endIndex - startIndex + 1 >= 3) { var item = new List<int>(); item.Add(startIndex); item.Add(endIndex); result.Add(item); } startIndex = i; } last = S[i]; } return result; } private static IList<IList<int>> LargeGroupPositions2(string S) { var result = new List<IList<int>>(); for(var i = 0; i < S.Length; i++) { var next = i + 1; var dic = new Dictionary<int, int>(); while(next < S.Length && S[next] == S[i]) { if(next - i >= 2) { dic[i] = next; } next++; } if(dic.TryGetValue(i, out int value)) { result.Add(new int[] { i, value }); i = next - 1; //循环内部更改循环计数是一种不好的做法,切记切记 //如果不是必需,请勿模仿此做法 } } return result; } }
以上给出2种算法实现,以下是这个案例的输出结果:
3 6 3 5 6 9 12 14
分析:
比较简单的一道题,主要是处理好边界,需要细心调试,LargeGroupPositions的时间复杂度为: ;LargeGroupPositions2的时间复杂度也为:
,虽然有内部while循环,但是计数被后移了,数组实际是还是被扫描了一次。
另外,循环内部更改循环计数是一种不好的做法,如果不是必需,请勿模仿此做法,切记!
本文由 .Net中文网 原创发布,欢迎大家踊跃转载。
转载请注明本文地址:https://www.byteflying.com/archives/3750。