[InterviewBit] Repeating Sub-Sequence

Repeating Sub-Sequence

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int Solution::anytwo(string A) {
unordered_map<char, vector<int>> mp;
for(int i = 0; i < A.length(); i++) mp[A[i]].push_back(i);
for(char a = 'a'; a <= 'z'; a++) {
for(char b = 'a'; b <= 'z'; b++) {
if(mp[a].size() < 2) continue;
if(mp[b].size() < 2) continue;
if(a == b) {
if(mp[a].size() >= 3) return true;
} else {
auto& A = mp[a];
auto& B = mp[b];
if(A[0] < B[B.size() - 2] and A[1] < B[B.size() - 1]) return true;
}
}
}
return 0;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/07/PS/interviewbit/repeating-subsequence/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.