Posts

Showing posts from September 2, 2024

Solution 2_ Java Program to Count and Map Character Reversals in a String- Interview Question Asked in TCS, Wipro, Deloitte, Tech-Mahindra

Image
### Explanation of the Java Program This Java program counts the frequency of each character in a given string and stores the results in a `HashMap`. The key of the `HashMap` represents the character, and the value represents the number of times that character appears in the string. #### Detailed Breakdown of the Code: 1. **Initialization**: - The input string `str` is initialized with the value `"deepakkumar"`. - The string is converted to a character array `ch` using the method `toCharArray()`. This array will be used to iterate over each character in the string. 2. **Creating the HashMap**: - A `HashMap map` is instantiated to store each character (as the key) and its frequency (as the value). 3. **Iterating Over Characters**: - A `for` loop is used to iterate over each character `c` in the character array `ch`. - For each character `c`, the program checks if it is already present in the `HashMap` using `map.containsKey(c)`. 4. **U...

Solution 1_ Java Program to Count and Map Character Reversals in a String- Interview Question Asked in TCS, Wipro, Deloitte, Tech-Mahindra

Image
### Brief Explanation of the Java Code The provided Java program analyzes a given string to count the occurrences of each character when comparing it with the reversed sequence of the string. The result is stored in a `HashMap` that maps each character to its calculated count. #### Step-by-Step Breakdown of the Code: 1. **Initialization**: - The input string `str` is initialized with the value `"deepakkumar"`. - A `StringBuilder sb` is created to build intermediate results while checking character occurrences. - The string `str` is converted to a character array `ch` using `toCharArray()`, which allows the program to process each character individually. - A `HashMap map` is created to store each character and its calculated count. 2. **Main Loop to Process Characters**: - The outer `for` loop iterates over each character in the character array `ch` using index `j`. - **Nested Loop for Reverse Comparison**: - For each character `c...