[Java] Print characters having prime frequencies in order of occurrence.

Question : Print characters having prime frequencies in order of occurrence

Given a string str containing only lowercase characters. The task is to print the characters having prime frequency in the order of their occurrence. Note that repeated elements with prime frequencies are printed as many times as they occur in order of their occurrence.

Examples:

  • Input: str = “facingissuesonit”
    Output: facingissuesonit Frequency
    ‘f’ 1
    ‘a’ 1
    ‘c’ 1
    ‘i’ 3
    ‘n’ 2
    ‘g’ 1
    ‘s’ 3
    ‘u’ 1
    ‘e’ 1
    ‘o’ 1
    ‘t’ 1

    ‘i’, ‘n’ and ‘s’ are the only characters with prime frequencies.
    Output: insinsis

  • Input: str = “aeroplane”
    Output: aeae

Algorithm

  • Step 1: Ask the user to input String
  • Step 2: Get count of each character occurrence and insert in character-based sorted map
  • Step 3: Remove Character those occurrences are not Prime
  • Step 4: Print Character in alphabetical order and reduce the count by one
  • Step 5: Remove an entry from the map if the count is zero
  • Step 6: Repeat step 4 and 5 until map get empty

Java Program

 

public class Program2 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
System.out.print("Enter a text String: ");
String inputStr = input.nextLine();
System.out.println("Result :" + primePlacesString(inputStr));
}

private static List primePlacesString(String inputStr) {
List charList = null;
char[] chars = inputStr.toCharArray();
// Enter Character and respected occurence in map
TreeMap map = new TreeMap();
for (Character c : chars) {
if (map.get(c) == null) {
map.put(c, 1);
} else {
map.put(c, map.get(c).intValue() + 1);
}
}
// Removed character those occurence are not prime
Character[] keys = (Character[]) map.keySet().toArray(new Character[map.size()]);
for (Character key : keys) {
if (!isPrimeNumber(map.get(key))) {
map.remove(key);
}
}
// get list of character in sequence as per counts
if (map != null && !map.isEmpty()) {
charList = new ArrayList();
printMapInSquence(map, charList);
}
return charList;
}

// Code to check number is prime or not
private static boolean isPrimeNumber(Integer number) {
boolean isPrimeNumber = true;
if (number == 0 || number == 1) {
isPrimeNumber = false;
} else {
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
isPrimeNumber = false;
break;
}
}
}

return isPrimeNumber;
}

private static void printMapInSquence(Map map, List charList) {
if (map != null && !map.isEmpty()) {

Character[] keys = (Character[]) map.keySet().toArray(new Character[map.size()]);
for (Character key : keys) {
charList.add(key);
if (map.get(key) == 1) {
// remove characters those are already printed and count are 1
map.remove(key);
} else {
// reduce counts of printed characters and counts more than one
map.put(key, map.get(key) - 1);
}
}
printMapInSquence(map, charList);
}
}

}

Output


Enter a text String: facingissuesonit
Result :[i, n, s, i, n, s, i, s]

Enter a text String: aeroplane
Result :[a, e, a, e]