Print Character by its Corresponding Count: This Java program takes an input string “1B3A2D4C” and produces an output string “BAAADDCCCC”. The program interprets the input as a combination of letters and digits, where each letter is followed by its corresponding count of occurrences. It then reconstructs the output string based on this information.
Print the Character by its Corresponding Count
package com.softwaretestingo.interviewprograms; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class InterviewPrograms20 { /* * Input 1B3A2D4C * Output BAAADDCCCC * WALLMART */ public static void main(String[] args) { String s = "1B3A2D4C"; String[] sArray = s.split(""); List<String> intList = new ArrayList<>(); List<String> sList = new ArrayList<>(); for(String temp : sArray) { if(temp.matches("[a-zA-Z]")) { sList.add(temp); } else { intList.add(temp); } } Map<String, Integer> map = new LinkedHashMap<>(); Iterator<String> itr1 = sList.iterator(); Iterator<String> itr2 = intList.iterator(); while(itr1.hasNext() && itr2.hasNext()) { map.put(itr1.next(), Integer.parseInt(itr2.next())); } for(Map.Entry<String, Integer> m : map.entrySet()) { for(int i=0;i<m.getValue();i++) { System.out.print(m.getKey()); } } } }
Output
BAAADDCCCC
Let’s break down the program section by section for Java beginners:
- Package and Import Statements:
- The program is part of the package com.softwaretestingo.interviewprograms.
- It imports several classes from the java.util package, including ArrayList, Iterator, LinkedHashMap, List, and Map.
- Class and main() Method:
- The program defines a class called InterviewPrograms20.
- It contains a main method, the entry point of the program, which will be executed first.
- Input String:
- String s = “1B3A2D4C”;: This line declares and initializes the input string s with the value “1B3A2D4C”.
- Splitting the Input String:
- String[] sArray = s.split(“”);: The input string s is split into an array of individual characters using the split(“”) method.
- Separating Digits and Letters:
- The program creates two lists: intList to store digits and sList to store letters.
- It uses an enhanced for loop to iterate over each character in sArray.
- If the character is a letter (checked using regex [a-zA-Z]), it is added to sList.
- If the character is a digit, it is added to intList.
- Creating a LinkedHashMap:
- Map<String, Integer> map = new LinkedHashMap<>();: A LinkedHashMap called map is created to store letters as keys and their corresponding counts (as integers) as values.
- Populating the LinkedHashMap:
- The program simultaneously uses two separate iterators (itr1 and itr2) to iterate over sList and intList.
- It pairs each letter with its corresponding count from the input string and puts them into the map.
- Reconstructing the Output String:
- The program uses a for-each loop to iterate over each entry (m) in the map.
- For each entry, it prints the letter m.getKey() as many times as specified by the count m.getValue().
- This reconstruction of letters and counts results in the output string “BAAADDCCCC” based on the input “1B3A2D4C”.
In summary, this Java program takes an input string with letter-count combinations, separates the letters and counts, stores them in a LinkedHashMap, and then reconstructs the output string by repeating each letter according to its corresponding count. The resulting output string is “BAAADDCCCC” for the given input “1B3A2D4C”.
Alternative:
This Java program aims to transform a given input string according to a specific pattern and output the modified string. Let’s break down the program step by step for beginners:
package com.softwaretestingo.interviewprograms; public class InterviewPrograms20_1 { // Interview Questions Asked In Unify technologies // Suppose input string is "c3d4a2" then output should be "cccddddaa" public static void main(String[] args) { String s="c3d4a2"; String ans=""; for (int i = 0; i<=s.length()-1; i++) { String r=String.valueOf(s.charAt(i)); int rep= Integer.valueOf(String.valueOf(s.charAt(i+1))); for (int j =1 ; j<=rep; j++) { ans=ans+r; } i=i+1; } System.out.println("The Inputted Parameter Is: "+s); System.out.print("Expected output Is: "+ans); } }
Output:
The Inputted Parameter Is: c3d4a2 Expected output Is: cccddddaa
Problem Description: The program deals with a scenario where an input string, such as “c3d4a2”, is provided. The task is to expand the string by repeating each character a certain number of times as indicated by the numeric value following the character.
Main Method: The program begins by defining a class named InterviewPrograms20_1 and the main method within it.
String Initialization: A string variable ‘s’ is initialized with the input value “c3d4a2”.
Initialization: An empty string variable ‘ans’ is declared. This variable will store the transformed string as the program progresses.
Outer Loop: The program enters a loop that iterates through the characters of the input string using the variable ‘i’. This loop is responsible for processing each character and its associated numeric value.
Current Character: Inside the outer loop, the program extracts the current character using s.charAt(i) and converts it into a string ‘r’. This will be the character that needs to be repeated.
Repetition Value: The numeric value following the current character is extracted by converting s.charAt(i+1) into an integer ‘rep’. This value indicates how many times the character should be repeated.
Inner Loop: Within the outer loop, there’s an inner loop that iterates ‘rep’ times (the repetition value). In each iteration, the character ‘r’ is added to the ‘ans’ string. This effectively repeats the character ‘rep’ times.
Index Update: After the inner loop completes, the index ‘i’ is incremented by 1 (i=i+1). This is done to skip the numeric value character that has already been processed.
Print Result: Once the loops finish, the program prints the value of the ‘ans’ string, which represents the expanded string according to the given pattern.
In summary, this program takes an input string containing characters followed by numeric values and expands it by repeating each character the specified number of times. It uses nested loops to process each character and its repetition value, then generates the transformed string and displays it as output.
Leave a Reply