String Manipulation Interview Questions
String Manipulation Interview Questions
To find occurrences of each word, split the string into words using spaces as delimiters. Utilize a HashMap to store each word as a key and its frequency as the value. For each word in the array, update its count in the map. This map provides word frequencies upon completion .
To remove duplicate characters, convert the string into a LinkedHashSet, which automatically handles duplicates. Then, iterate over the set to build a new string. This guarantees preserved order without duplicates, effectively filtering the original string .
To reverse a string using a third variable, initialize an empty string as the third variable. Iterate over the original string from the end to the start, appending each character to the third variable. The result is a reversed string stored in the third variable, as demonstrated in the provided code .
To print only unique words from a string, split the string into words. Use a HashMap to track word occurrences. Iterate over the words to fill the map. Afterwards, print words whose occurrence count equals one, signifying uniqueness .
To remove duplicate words while preserving order, split the string by spaces to separate words. Use a LinkedHashSet to automatically filter out duplicates while maintaining order of insertion. Finally, concatenate the words in the set back into a string .
To reverse the words, split the sentence into words using spaces. Reverse the order of these words. Concatenate the reversed words into a new string, separated by spaces. This preserves the original word order while reversing their positions, as shown in assignment examples .
To print only the duplicate characters, use a HashMap to maintain counts of each character. Iterate over the string, updating the count in the map. After processing, iterate through the map and print characters with counts greater than one, meaning they appear more than once in the string .
First, convert the string to a character array and manually count the characters using a loop to determine length. Use this length to iterate backwards over the character array, printing each character, which effectively reverses the string .
To find the occurrences of each character, first, create a HashSet to store each unique character from the string. Then, iterate over the HashSet and compare each character with those in the string. If a match occurs, increment a count. This will give you the frequency of each character .
To reverse a string without using a third or temporary variable, you would use a two-pointer approach. You swap characters starting from the beginning and end of the string moving towards the center. For a string "India", iterate over the string with pointers moving towards the center, swapping elements at the pointers, until all characters are reversed.