Given a string and an integer M, print all distinct circular strings of length M in lexicographical order.
Input: str = “baaaa”, M = 3
Output: aaa aab aba baa
All possible circular substrings of length 3 are “baa” “aaa” “aaa” “aab” “aba”
Out of the 6, 4 are distinct, and the lexicographical order is aaa aab aba baa
Input: str = “saurav”, M = 3
Output: aura avsa ravs saur urav vsau
All possible circular substrings of length 4 are saur aura urav ravs avsa vsau.
All the substrings are distinct, the lexicographical order is aura avsa ravs saur urav vsau.
Approach: The substr function is used to solve the problem. Append the string to itself at first. Iterate over the length of the string to generate all possible substrings of length M. Set is used in C++ to store all the distinct substrings of length 4, set by default stores all its element in lexicographical order. Once all the strings are generated, print the elements in the set from the beginning.
Below is the implementation of the above approach:
aura avsa ravs saur urav vsau
Time Complexity: O(N*M), where N is the length of the string.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print all the combinations of a string in lexicographical order
- Print all longest common sub-sequences in lexicographical order
- Print all distinct characters of a string in order (3 Methods)
- Print common characters of two Strings in alphabetical order
- Print Strings In Reverse Dictionary Order Using Trie
- Print array of strings in sorted order without copying one string into another
- Python code to print common characters of two Strings in alphabetical order
- Generate all numbers up to N in Lexicographical Order
- Find the k-th string in lexicographical order consisting of n-2 X's and 2 Y's
- Sort the words in lexicographical order in Python
- K-th lexicographical string of given length
- Print all lexicographical greater permutations of a given string
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Count of same length Strings that exists lexicographically in between two given Strings
- Distinct strings such that they contains given strings as sub-sequences
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Generating distinct subsequences of a given string in lexicographic order
- Distinct strings with odd and even changes allowed
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Count number of distinct substrings of a given length
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.