This is one of those problems where the clean idea matters more than the amount of code. For 830. Positions of Large Groups, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The solution is organized around the main LeetCode entry point and a few local helpers.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02In a string S of lowercase letters, these letters form consecutive groups of the same character.
03
04For example, a string like S = "abbxxxxzyy" has the groups "a", "bb", "xxxx", "z" and "yy".
05
06Call a group large if it has 3 or more characters. We would like the starting and ending positions of every large group.
07
08The final answer should be in lexicographic order.
09
10
11
12Example 1:
13
14Input: "abbxxxxzzy"
15Output: [[3,6]]
16Explanation: "xxxx" is the single large group with starting 3 and ending positions 6.
17Example 2:
18
19Input: "abc"
20Output: []
21Explanation: We have "a","b" and "c" but no large group.
22Example 3:
23
24Input: "abcdddeeeeaabbbcd"
25Output: [[3,5],[6,9],[12,14]]
26
27
28Note: 1 <= S.length <= 1000
29**/
30
31//Runtime: 8 ms, faster than 100.00% of C++ online submissions for Positions of Large Groups.
32//Memory Usage: 9.1 MB, less than 100.00% of C++ online submissions for Positions of Large Groups.
33
34class Solution {
35public:
36 vector<vector<int>> largeGroupPositions(string S) {
37 int start = 0, end = 0;
38 char c = '\0';
39 vector<vector<int>> ans;
40
41 for(int i = 0; i < S.size(); i++){
42 if(S[i] != c){
43 //last group
44 if(end - start >= 2){
45 //length > 3 group
46 ans.push_back(vector<int> {start, end});
47 }
48 //update for current group
49 c = S[i];
50 start = i;
51 end = i;
52 }else{
53 end = i;
54 }
55 // cout << c << " " << start << " " << end << endl;
56 }
57
58 if(end - start >= 2){
59 //length > 3 group
60 ans.push_back(vector<int> {start, end});
61 }
62
63 return ans;
64 }
65};
66
67/**
68Approach #1: Two Pointer [Accepted]
69Intuition
70
71We scan through the string to identify the start and end of each group. If the size of the group is at least 3, we add it to the answer.
72
73Algorithm
74
75Maintain pointers i, j with i <= j. The i pointer will represent the start of the current group, and we will increment j forward until it reaches the end of the group.
76
77We know that we have reached the end of the group when j is at the end of the string, or S[j] != S[j+1]. At this point, we have some group [i, j]; and after, we will update i = j+1, the start of the next group.
78**/
79
80/**
81Complexity Analysis
82
83Time Complexity: O(N)O(N), where NN is the length of S.
84
85Space Complexity: O(N)O(N), the space used by the answer.
86**/
87
88//Runtime: 8 ms, faster than 100.00% of C++ online submissions for Positions of Large Groups.
89//Memory Usage: 9.3 MB, less than 100.00% of C++ online submissions for Positions of Large Groups.
90/**
91class Solution {
92public:
93 vector<vector<int>> largeGroupPositions(string S) {
94 int start = 0, end = 0;
95 vector<vector<int>> ans;
96
97 for(int end = 0; end < S.size(); end++){
98 //check for a group when the string ends
99 if(end == S.size()-1 || S[end] != S[end+1]){
100 //deal with old group
101 if(end-start+1 >= 3){
102 ans.push_back(vector<int> {start, end});
103 }
104 //update for new group
105 start = end+1;
106 }
107 }
108
109 return ans;
110 }
111};
112**/
Cost