HackerRank : World CodeSprint 7- Two Characters

By | September 25, 2016

String always consists of two distinct alternating characters. For example, if string ‘s two distinct characters are xand y, then t could be xyxyx or yxyxy but not xxyy or xyyx.

You can convert some string to string by deleting characters from . When you delete a character from , you must delete all occurrences of it in . For example, if abaacdabd and you delete the character a, then the string becomes bcdbd.

Given , convert it to the longest possible string . Then print the length of string on a new line; if no string can be formed from , print instead.

Input Format

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

Constraints

  • only contains lowercase English alphabetic letters (i.e., a to z).

Output Format

Print a single integer denoting the maximum length of for the given ; if it is not possible to form string , print instead.

Sample Input

10
beabeefeab

Sample Output

5

Explanation

The characters present in are a, b, e, and f. This means that must consist of two of those characters.

If we delete e and f, the resulting string is babab. This is a valid as there are only two distinct characters (a andb), 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 three consecutivee‘s present.

If we delete only e, the resulting string is babfab. This is not a valid string because it contains three distinct characters.

Thus, we print the length of babab, which is , as our answer.

Solution

I have tried initial code. It is passing few test cases. Will be uploading full once its done.

 Scanner scan = new Scanner(System.in);
 int n = scan.nextInt();

 scan.nextLine();
 String s = scan.nextLine();

 char sArr[] = s.toCharArray(); // char array
 Set<String> set = new HashSet<String>();

 for (char c : sArr) {

 String sChar = String.valueOf(c);
 set.add(sChar);
 }

 List<String> list = new ArrayList<String>();
 list.addAll(set);
 int max = 0;

 int lengthSet = set.size();
 for (int i = 0; i < lengthSet - 1; i++) {
 for (int j = i + 1; j < lengthSet; j++) {
 max = Integer.MIN_VALUE;
 // System.out.print("i= "+list.get(i)+ " j= "+list.get(j));
 char sArr2[] = sArr.clone();
 // code logic starts

 // System.out.println();
 for (int p = 0; p < sArr2.length; p++) {
 char c = sArr2[p];
 char iChar = list.get(i).charAt(0);
 char jChar = list.get(j).charAt(0);

 if (c == iChar || c == jChar) {
 sArr2[p] = '#';
 }
 }
 StringBuilder sB = new StringBuilder();

 for (int g = 0; g < sArr.length; g++) {
 char c = sArr2[g];
 if (c != '#') {
 sB.append(c);
 }
 }
 // System.out.println(set);
 String finalString = sB.toString();
 // System.out.println("sb "+sB);

 boolean resultStr = validate(finalString);
 // System.out.println("finalString "+ finalString +
 // " resultStr "+ resultStr);
 if (resultStr == true) {
 // System.out.println("finalString "+finalString);
 int len = finalString.length();
 if (len > max)
 max = len;
 }
 // code logic ends

 }
 }

 if (max < 0)
 System.out.println(0);
 else
 System.out.println(max);

public class Solution {
static boolean validate(String s)
 {
 boolean isvalidate=true;
 char sArr[]= s.toCharArray();
 char a=sArr[0];
 char b=sArr[1];
 
 for (int i = 0; i < sArr.length-1; i=i+2) {
 
 if(sArr[i]==a & sArr[i+1]==b)
 {}
 else
 {
 isvalidate=false;
 break;
 }
 char c = sArr[i];
 
 }
 
 return isvalidate;

 }
 public static void main(String[] args) {
 Scanner scan= new Scanner(System.in);
 int n= scan.nextInt();
 
 scan.nextLine();
 String s= scan.nextLine();
 
 char sArr[]= s.toCharArray(); // char array 
 Set<String> set= new HashSet<String>();
 
 for (char c : sArr) {
 
 String sChar=String.valueOf(c);
 set.add(sChar);
 }
 
 List<String> list= new ArrayList<String>();
 list.addAll(set);
 int max=0;
 
 int lengthSet=set.size();
 for(int i=0;i<lengthSet-1;i++)
 {
 for(int j=i+1;j<lengthSet;j++)
 {
 max=Integer.MIN_VALUE;
 // System.out.print("i= "+list.get(i)+ " j= "+list.get(j));
 char sArr2[]= sArr.clone();
 //code logic starts
 
 // System.out.println();
 for (int p = 0; p < sArr2.length; p++) {
 char c = sArr2[p];
 char iChar=list.get(i).charAt(0);
 char jChar=list.get(j).charAt(0); 
 
 if(c==iChar || c==jChar)
 {
 sArr2[p]='#';
 }
 }
 StringBuilder sB=new StringBuilder();
 
 for (int g = 0; g < sArr.length; g++) {
 char c = sArr2[g];
 if(c!='#')
 {
 sB.append(c);
 } 
 }
 // System.out.println(set);
 String finalString = sB.toString();
 //System.out.println("sb "+sB);
 
 boolean resultStr= validate(finalString);
 // System.out.println("finalString "+ finalString + " resultStr "+ resultStr);
 if(resultStr==true)
 {
 // System.out.println("finalString "+finalString);
 int len=finalString.length();
 if(len>max)
 max=len;
 }
 
 
 // code logic ends
 
 }
 
 
 }
 
 if(max<0)
 System.out.println(0);
 else
 System.out.println(max);
 
 //System.out.println("hi");
 
 }
}