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.

Example

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. Return .

Given a string , convert it to the longest possible string  made up only of alternating characters. Return the length of string . If no string  can be formed, return .

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  if there are none

Input Format

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

Constraints

Sample Input

STDIN       Function
-----       --------
10          length of s = 10
beabeefeab  s = 'beabeefeab'

Sample Output

5

Explanation

The characters present in  are abe, and f. This means that  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  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  because there are consecutive e's present. Removing them would leave consecutive b's, so this fails to produce a valid string .

Other cases are solved similarly.

babab is the longest string we can create.

import java.io.*;
import java.lang.reflect.Array;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

/*
* Complete the 'alternate' function below.
*
* The function is expected to return an INTEGER.
* The function accepts STRING s as parameter.
*/

public static int alternate(String s) {
Integer[] count = new Integer[26];
Arrays.fill(count, 0);
for (int i = 0; i < s.length(); i++) {
count[s.charAt(i)-97]++;
}

int max = 0;

for (int i = 0; i < count.length-1; i++) {
for (int j = i+1; j < count.length; j++) {
int currentCount = count[i]+count[j];
if (currentCount > max && (count[i]!=0 && count[j]!=0)) {
Character letter1 = (char)(i+97);
Character letter2 = (char)(j+97);
String double1 = letter1+""+letter1;
String double2 = letter2 +""+letter2;

String temp = "";

for (int k = 0; k < s.length(); k++) {
if (s.charAt(k) == letter1 || s.charAt(k) == letter2) {
temp+= s.charAt(k);
}
}

if (temp.contains(double1) || temp.contains(double2) ) {

}else{
max = currentCount;
}


}
}
}


return max;
}

}

class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));

int l = Integer.parseInt(bufferedReader.readLine().trim());

String s = bufferedReader.readLine();

int result = Result.alternate(s);

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();

bufferedReader.close();
bufferedWriter.close();
}
}


 

Comments

Popular posts from this blog

Hackerrank - Quicksort 2 - Sorting

Hackerrank - Day of the Programmer

Hackerrank - UNIQUE ARMSTRONG NUMBER