Category Archives: Strings

Sort strings in Lexicographical order

You are given n strings. You have to print them in lexicographical increasing order. Also, ensure that all of the strings should be distinct. Examples: Input: {“Aarav”, “Arjun”, “Rahul”, “Rishabh”, “Kunal”} Output: {“Aarav”, “Arjun”, “Kunal”, “Rahul”, “Rishabh”} Input: {“Vikram”, “Rajat”, “Abhishek”, “Suresh”, “Rajat”} Output: {“Abhishek”, “Rajat”, “Suresh”, “Vikram”} Recommended: What is Algorithm | Introduction to Algorithms Approach: Actually… Read More »

Sort an array of string of dates in ascending order

Given an array arr[] of N dates in the form of “dd-mm-yyyy”, the task is to sort these dates in ascending order. Examples: Input: arr[] = { “25-08-1996”, “03-08-1970”, “09-04-1994” } Output: 03-08-1970 09-04-1994 25-08-1996 Input: arr[] = { “14-02-1972”, “01-01-2001”} Output: 14-02-1972 01-01-2001 A simple solution that comes to our mind is to delete all ‘-‘ from… Read More »

Find length of Longest Consecutive Occurrences of second string that occurs in first string

You are given two strings string1 and string2. Your task is to find length of longest consecutive occurrences of second string that occurs in first string. Examples: Input: string1=”AAACBBAAAA”,  string2=”A” Output: 4 Explanation: We will get AAA(0:3) and then AAAA(6:10) as 4 is greater than 3, answer is 4. Input: string1=”ABBABBABBAAAA”,  string2=”ABB” Output: 9 Approach: Initially find length… Read More »

Sort odd indices in ascending order and even indices in descending order of the string

Given a string S, the task is to sort odd indices in ascending order and even indices in descending order of the string. Examples: Input: S = “Cplusplus” Output: Cululspsp Explanation: Sort string S in ascending order: S = “Cululspsp” Replace odd indices in ascensing order and even indices in descending order. Input: S = “plusplus” Output: lulupsps… Read More »

Smallest window in a String containing all characters of other String

Given two strings string1 and string2, the task is to find the smallest substring in string1 containing all characters of string2. Examples : Input: string = “Cplusplus is the best”, pattern = “pp” Output: Smallest window is : plusp Explanation: “plusp” contains all the characters of pattern. Input: string = “Cplusplus”, pattern = “Cpp” Output: Smallest window is… Read More »