# Two Characters HackerRank Solution in C, C++, Java, Python

Given a string, remove characters until the string is made up of any two alternating characters. When you choose a character to remove, all instances of that character must be removed. Determine the longest string possible that contains just two alternating letters.

**Example88.

$s = \text{abaacdabd}$

Delete a, to leave bcdbd. Now, remove the character c to leave the valid string bdbd with a length of 4. Removing either b or d at any point would not result in a valid string.

Given a string $s$, convert it to the longest possible string $t$ made up only of alternating characters. Print the length of string $t$ on a new line. If no string $t$ can be formed, print $0$ instead.

Function Description

Complete the alternate function in the editor below.

alternate has the following parameter(s):

• string s: a string

Returns.

• int: the length of the longest valid string, or $0$ if there are none

Input Format

The first line contains a single integer that denotes the length of s.

The second line contains string s.

Constraints

• 1<=lengthofs<=1000
• S[i] belongs to ascii[a-z]

Sample Input

10

beabeefeab

Sample Output

5

Explanation

The characters present in s are a, b, e, and f. This means that t must consist of two of those characters and we must delete two others. Our choices for characters to leave are [a,b], [a,e], [a, f], [b, e], [b, f] and [e, f].

If we delete e and f, the resulting string is babab. This is a valid t as there are only two distinct characters (a and b), and they are alternating within the string.

If we delete a and f, the resulting string is bebeeeb. This is not a valid string t because there are consecutive e’s present. Removing them would leave consecutive b’s, so this fails to produce a valid string t.

Other cases are solved similarly.

babab is the longest string we can create.

## Two Characters HackerRank Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int calculate(char one,char two,char *in){
int length=strlen(in);
int a;
int flag=0;
int count=0;
for(a=0;a<length;++a){
if(in[a]==one || in[a]==two){
count+=1;
if(in[a]==one){
if(flag==-1){
return 0;
}
else{
flag=-1;
}
}
if(in[a]==two){
if(flag==1){
return 0;
}
else{
flag=1;
}
}
}
}

return count;
}
int main() {
int n,a;
scanf("%d",&n);
char in;
scanf("%s",in);
char list;
for(a=0;a<26;++a){
list[a]=0;
}
int length=strlen(in);
for(a=0;a<length;++a){
list[in[a]-'a']=1;
}
char one,two;
int b,value,max;
for(a=0;a<26;++a){
if(list[a]==1){
for(b=a+1;b<26;++b){
if(list[b]==1){
one = a + 'a';
two = b + 'a';
value=calculate(one,two,in);
if(value>max){
max=value;
}
}
}

}

}
printf("%d",max);

return 0;
}

## Two Characters HackerRank Solution in C++

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

int valid(string x) {
const int n = x.size();
for (int i = 1; i < n; ++i)
if (x[i] == x[i-1])
return false;
return true;
}

int main() {
int asd;
cin>>asd;
string s;
cin>>s;
int ans = 0;
for (char a = 'a'; a <= 'z'; ++a)
for (char b = 'a'; b <= 'z'; ++b)
if (a != b)
{
if (s.find(a) == string::npos) continue;
if (s.find(b) == string::npos) continue;
string x;
for (const char ch : s)
if (ch == a || ch == b)
x.push_back(ch);
if (valid(x))
ans = max(ans, (int)x.size());
}
printf("%d\n", ans);
}

## Two Characters HackerRank Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
String s=in.next();
String res="";
for(int i=0;i<26;i++){
for(int j=i+1;j<26;j++){
char a=(char)('a'+i);
char b=(char)('a'+j);
String cur="";
for(int k=0;k<n;k++){
if (s.charAt(k)==a || s.charAt(k)==b) {
cur+=s.charAt(k);
}
}
if (cur.length()<res.length()) continue;
if (isGood(cur)) res=cur;
}
}
System.out.println(res.length());
}
public static boolean isGood(String s){
if (s.length()==1) return false;
for(int i=1;i<s.length();i++){
if (s.charAt(i)==s.charAt(i-1)) return false;
}
return true;
}
}

## Two Characters HackerRank Solution in Python

from collections import Counter
from itertools import combinations
def is_valid(S):
c = Counter(S)
#print c
if len(c) != 2:
return False
for i in xrange(1, len(S)):
if S[i] == S[i-1]:
return False
return True

def keep_letters(lista, keep):
return filter(lambda x: x in keep, lista)

N = int(raw_input())
S = list(raw_input().strip())

letters = {x: 1 for x in S}
letters = list(combinations(letters.keys(), 2))
#print letters
L = list(S)
first = True
m = 0
for keep in letters:
#print list(S)
lista = keep_letters(list(S), keep)
#print lista
if is_valid(lista):
m = max(m, len(lista))

print m



## Two Characters HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.Linq;
class Solution {
static void Main(String[] args) {

if (n == 1) { Console.WriteLine(0); return; }
int ans = 0;
for (int i = 0; i < 26; i++) {
for (int j = 0; j < 26; j++) {
if (i == j) continue;
ans = Math.Max(ans, alternating(line, (char)('a' + i), (char)('a' + j)));
}
}
Console.WriteLine(ans);
}

static int alternating(string line, char p1, char p2) {
bool first = true;
int r = 0;
foreach (var c in line.Where(x=>x==p1 || x == p2)) {
if (first) {
if (c == p2) return -1;
r++;
} else {
if (c == p1) return -1;
r++;
}
first = !first;
}
return r;
}
}

Attempt Two Characters HackerRank Challenge

Next HackerRank Challenge Solution