-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathE0105.cpp
More file actions
56 lines (47 loc) · 1.13 KB
/
E0105.cpp
File metadata and controls
56 lines (47 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Problem Statement: https://www.hackerrank.com/challenges/two-characters/problem
*/
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
int alternate(string &s) {
int len = 0, n = 26;
vector< vector<int> > cnt(n, vector<int>(n));
vector< vector< pair<char, char> > > comb(n, vector< pair<char, char> >(n));
// Initialization
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
comb[i][j] = make_pair('a' + i, 'a' + j);
if (i == j)
cnt[i][j] = -1;
}
// Check for each combination in string
for (int k = 0; k < s.length(); k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (cnt[i][j] == -1)
continue;
else if (s[k] == comb[i][j].first) {
swap(comb[i][j].first, comb[i][j].second);
cnt[i][j]++;
}
else if(s[k] == comb[i][j].second)
cnt[i][j] = -1;
}
// Calculate max length
for (int i = 0; i < n; i++)
len = max(len, *max_element(cnt[i].begin(), cnt[i].end()));
if (len == 1)
len = 0;
return len;
}
int main() {
int n;
string s;
cin >> n >> s;
cout << alternate(s);
return 0;
}