Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://www.hackerrank.com/challenges/two-characters/problem
- // Wrong answer - How can I fix this?
- #include <bits/stdc++.h>
- using namespace std;
- bool check(string s){
- for(int i=1;i<s.size();i++)
- if(s[i] == s[i-1])
- return false;
- return true;
- }
- int main() {
- string S;
- cin >> S;
- bool ap[300];
- memset(ap,false,sizeof ap);
- int diff_letters = 0;
- for(int i=0;i<S.size();i++)
- if(!ap[ S[i]-'a' ]){
- ap[ S[i]-'a' ] = true;
- diff_letters++;
- }
- // cout << diff_letters << endl;
- if(diff_letters == 1){
- printf("0\n");
- return 0;
- }
- int ans=0;
- for(int i=0; i<26; i++){
- for(int j=0; j<26; j++){
- if( (ap[i] && ap[j]) && (i != j) ){
- // cout << i << " " << j << endl;
- string new_s = "";
- for(int k=0; k<S.size(); k++)
- if( (S[k] == i+'a') || (S[k] == j+'a') )
- new_s += S[k];
- if(check(new_s)){
- int tam = new_s.size();
- ans = max(ans,tam);
- }
- }
- }}
- printf("%d\n",ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement