package abc;
import java.util.ArrayList;
public class CharRepeatCount {
public static void main(String[] args) {
// Find the character repeated maximum number of times.
// if you are given word = aaabbbddddmmmmmsszzz then return 5m
String word = "aaabbbddddmmmmmsszzz";
int wordLength = word.length();
int maxCounter = 0;
System.out.println("wordLength = " +wordLength);
int counter = 1;
int z = 0;
int j = 1;
//To find the count of each char in digit only
ArrayList<Integer>
numArray = new ArrayList<>();
ArrayList<String>
charArray = new ArrayList<>();
for(int i=1;i<wordLength;i++) {
numArray.add(counter);
charArray.add(Character.toString(word.charAt(i-1)));
if((Character.toString(word.charAt(i-1)).equals(Character.toString(word.charAt(i))))) {
counter++ ;
}
else {
counter =1;
}
}
numArray.add(counter);//Because the last digit will not
be counted
charArray.add(Character.toString(word.charAt(wordLength-1))); //Because the last char will not be counted
System.out.println(numArray);
System.out.println(charArray);
//Find the max integer
int[] num = new int[wordLength];
String[]
strg = new String[wordLength];
for(int i=0;i<wordLength; i++) {
num[i]= numArray.get(i);
strg[i]= charArray.get(i);
}
Arrays.sort(num);
int maxNumber = num[wordLength-1] ;
System.out.println("The maximum number = "+Integer.toString(num[wordLength-1]));
//Find the char associated with the max integer
int index = numArray.indexOf(maxNumber);
System.out.println("The index = "+index);
System.out.println("The maximum repeated Char = "+strg[index]);
//output
String[]
expectedChar = {"a"};
expectedChar[0]= Integer.toString(num[wordLength-1]).concat(strg[index]);
System.out.println("The maximum repeated Char with Num = "+expectedChar[0]);
//OR
String[]
expectedCharr = new String[1];
expectedChar[0]= Integer.toString(num[wordLength-1]).concat(strg[index]);
System.out.println("The maximum repeated Char with Num = "+expectedChar[0]);
}
}
Output:
wordLength = 20
[1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 1, 2,
3, 4, 5, 1, 2, 1, 2, 3]
[a, a, a, b, b, b, d, d, d, d, m, m,
m, m, m, s, s, z, z, z]
The maximum number = 5
The index = 14
The maximum repeated Char = m
The maximum repeated Char with Num =
5m
The maximum repeated Char with Num =
5m
No comments:
Post a Comment