Category Archives: Strings

Questions related to strings

Check if edit distance between two strings is one

An edit between two strings is one of the following changes. Add a character Delete a character Change a character Given two string s1 and s2, find if s1 can be converted to s2 with exactly one edit. Expected time complexity is O(m+n) where m and n are lengths of two strings. Examples: Input: s1… Read More »

Reverse an array without affecting special characters

Given a string, that contains special character together with alphabets (‘a’ to ‘z’ and ‘A’ to ‘Z’), reverse the string in a way that special characters are not affected. Examples: Input: str = “a,b$c” Output: str = “c,b$a” Note that $ and , are not moved anywhere. Only subsequence “abc” is reversed Input: str =… Read More »

Length of the longest valid substring

Given a string consisting of opening and closing parenthesis, find length of the longest valid parenthesis substring. Examples: Input : ((() Output : 2 Explanation : () Input: )()()) Output : 4 Explanation: ()() Input: ()(())))) Output: 6 Explanation: ()(())) We strongly recommend you to minimize your browser and try this yourself first. A Simple… Read More »

Shortest Superstring Problem

Given a set of n strings arr[], find the smallest string that contains each string in the given set as substring. We may assume that no string in arr[] is substring of another string.