Category Archives: Strings

Questions related to strings

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 »

Hamming Distance between two strings

You are given two strings of equal length, you have to find the Hamming Distance between these string. Where the Hamming distance between two strings of equal length is the number of positions at which the corresponding character are different. Examples: Input : str1[] = “geeksforgeeks”, str2[] = “geeksandgeeks” Output : 3 Explanation : The… Read More »

Implementation of Affine Cipher

The Affine cipher is a type of monoalphabetic substitution cipher, wherein each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter. The formula used means that each letter encrypts to one other letter, and back again, meaning the cipher is essentially a… Read More »

Boggle | Set 2 (Using Trie)

Given a dictionary, a method to do lookup in dictionary and a M x N board where every cell has one character. Find all possible words that can be formed by a sequence of adjacent characters. Note that we can move to any of 8 adjacent characters, but a word should not have multiple instances… Read More »