forked from chencorey/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwo Characters.cpp
More file actions
64 lines (61 loc) · 1.47 KB
/
Two Characters.cpp
File metadata and controls
64 lines (61 loc) · 1.47 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
57
58
59
60
61
62
63
64
// https://www.hackerrank.com/contests/world-codesprint-7/challenges/two-characters
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int N;
cin>>N;
string word;
cin>>word;
int result = 0;
if(word.length()==1)
{
cout<<0;
return 0;
}
for(char a = 'a'; a<='z'; a++)
{
for(char b = 'a'; b<='z'; b++)
{
if(b==a)continue;
int best = 0;
bool first = true;
for(int i = 0; i<word.length(); i++)
{
if(first)
{
if(a==word[i])
{
first = false;
best++;
}
else if(b==word[i])
{
best = -1;
break;
}
}
else
{
if(a==word[i])
{
best = -1;
break;
}
else if(b==word[i])
{
first = true;
best++;
}
}
}
result = max(result, best);
}
}
cout<<result;
return 0;
}