Category Archives: Strings

Questions related to strings

Group words with same set of characters

Given a list of words with lower cases. Implement a function to find all Words that have the same unique character set . Example: Input: words[] = { “may”, “student”, “students”, “dog”, “studentssess”, “god”, “cat”, “act”, “tab”, “bat”, “flow”, “wolf”, “lambs”, “amy”, “yam”, “balms”, “looped”, “poodle”}; Output : looped, poodle, lambs, balms, flow, wolf, tab,… Read More »

Find largest word in dictionary by deleting some characters of given string

Giving a dictionary and a string ‘str’, find the longest string in dictionary which can be formed by deleting some characters of the given ‘str’. Examples: Input : dict = {“ale”, “apple”, “monkey”, “plea”} str = “abpcplea” Output : apple Input : dict = {“pintu”, “geeksfor”, “geeksgeeks”, ” forgeek”} str = “geeksforgeeks” Output : geeksgeeks… Read More »

Make largest palindrome by changing at most K-digits

Given a string containing all digits, we need to convert this string to a palindrome by changing at most K digits. If many solutions are possible then print lexicographically largest one. Examples: Input : str = “43435” k = 3 Output : “93939” Lexicographically largest palindrome after 3 changes is “93939” Input : str =… Read More »

Count of total anagram substrings

Given a string of lower alphabet characters, count total substring of this string which are anagram to each other. Examples: Input : str = “xyyx” Output : 4 Total substrings of this string which are anagram to each other are 4 which can be enumerated as, {“x”, “x”}, {“y”, “y”}, {“xy”, “yx”}, {“xyy”, “yyx”} Input… Read More »

Print all valid words that are possible using Characters of Array

Given a dictionary and a character array, print all valid words that are possible using characters from the array. Examples: Input : Dict – {“go”,”bat”,”me”,”eat”,”goal”, “boy”, “run”} arr[] = {‘e’,’o’,’b’, ‘a’,’m’,’g’, ‘l’} Output : go, me, goal. Asked In : Microsoft Interview The idea is to use Trie data structure to store dictionary, then search… Read More »

Print consecutive characters together in a line

Given a sequence of characters, print consecutive sequence of characters in a line, otherwise print it in a new line. Examples: Input : ABCXYZACCD Output : ABC XYZ A C CD Input : ABCZYXACCD Output: ABC ZYX A C CD The idea is to traverse string from left to right. For every traversed character, print… Read More »