Category Archives: Strings

Questions related to strings

Longest Common Prefix | Set 2 (Character by Character Matching)

Given a set of strings, find the longest common prefix. Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”} Output : “gee” Input : {“apple”, “ape”, “april”} Output : “ap” We have discussed word by word matching algorithm in previous post. In this algorithm, instead of going through the strings one by one, we will go through the… Read More »

Longest Common Prefix | Set 1 (Word by Word Matching)

Given a set of strings, find the longest common prefix. Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”} Output : “gee” Input : {“apple”, “ape”, “april”} Output : “ap” We start with an example. Suppose there are two strings- “geeksforgeeks” and “geeks”. What is the longest common prefix in both of them? It is “geeks”. Now let… Read More »

How to find Lexicographically previous permutation?

Given a word, find lexicographically smaller permutation of it. For example, lexicographically smaller permutation of “4321” is “4312” and next smaller permutation of “4312” is “4231”. If the string is sorted in ascending order, the next lexicographically smaller permutation doesn’t exist. We have discussed next_permutation() that modifies a string so that it stores lexicographically smaller… Read More »

In-place replace multiple occurrences of a pattern

Given a string and a pattern, replace multiple occurrences of a pattern by character ‘X’. The conversion should be in-place and solution should replace multiple consecutive (and non-overlapping) occurrences of a pattern by a single ‘X’. String – GeeksForGeeks Pattern – Geeks Output: XforX String – GeeksGeeks Pattern – Geeks Output: X String – aaaa… Read More »

Pangram Checking

Given a string check if it is Pangram or not. A pangram is a sentence containing every letter in the English Alphabet. Examples : The quick brown fox jumps over the lazy dog ” is a Pangram [Contains all the characters from ‘a’ to ‘z’] “The quick brown fox jumps over the dog” is not… Read More »

Generate all binary strings from given pattern

Given a string containing of ‘0’, ‘1’ and ‘?’ wildcard characters, generate all binary strings that can be formed by replacing each wildcard character by ‘0’ or ‘1’. Input str = “1??0?101” Output: 10000101 10001101 10100101 10101101 11000101 11001101 11100101 11101101 Method 1 (Using Recursion) We pass index of next character to the recursive function.… Read More »

Print all ways to break a string in bracket form

Given a string, find all ways to to break the given string in bracket form. Enclose each substring within a parenthesis. Examples: Input : abc Output: (a)(b)(c) (a)(bc) (ab)(c) (abc) Input : abcd Output : (a)(b)(c)(d) (a)(b)(cd) (a)(bc)(d) (a)(bcd) (ab)(c)(d) (ab)(cd) (abc)(d) (abcd) We strongly recommend you to minimize your browser and try this yourself… Read More »

K’th Non-repeating Character

Given a string and a number k, find the k’th non-repeating character in the string. Consider a large input string with lacs of characters and a small character set. How to find the character by only doing only one traversal of input string? Examples: Input : str = geeksforgeeks, k = 3 Output : r… Read More »