# Print all distinct circular strings of length M in lexicographical order

Given a string and an integer M, print all distinct circular strings of length M in lexicographical order.

**Examples: **

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:

`// C++ program to print all ` `// distinct circular strings ` `// of length M in lexicographical order ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print all the distinct substrings ` `// in lexicographical order ` `void` `printStrings(string s, ` `int` `l, ` `int` `m) ` `{ ` ` ` `// stores all the distinct substrings ` ` ` `set<string> c; ` ` ` ` ` `// Append the string to self ` ` ` `s = s + s; ` ` ` ` ` `// Iterate over the length to generate ` ` ` `// all substrings of length m ` ` ` `for` `(` `int` `i = 0; i < l; i++) { ` ` ` ` ` `// insert the substring of length m ` ` ` `// in the set ` ` ` `c.insert(s.substr(i, m)); ` ` ` `} ` ` ` ` ` `// prints all the distinct circular ` ` ` `// substrings of length m ` ` ` `while` `(!c.empty()) { ` ` ` ` ` `// Prints the substring ` ` ` `cout << *c.begin() << ` `" "` `; ` ` ` ` ` `// erases the beginning element after ` ` ` `// printing ` ` ` `c.erase(c.begin()); ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"saurav"` `; ` ` ` `int` `N = str.length(); ` ` ` `int` `M = 4; ` ` ` ` ` `printStrings(str, N, M); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

aura avsa ravs saur urav vsau

**Time Complexity**: O(N*M), where N is the length of the string.

## Recommended Posts:

- 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)
- Sort the words in lexicographical order in Python
- Print common characters of two Strings in alphabetical order
- 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
- Counts of distinct consecutive sub-string of length two using C++ STL
- Generating distinct subsequences of a given string in lexicographic order
- Distinct strings with odd and even changes allowed
- Smallest length string with repeated replacement of two distinct adjacent
- Length of the smallest sub-string consisting of maximum distinct characters
- Check if given string can be split into four distinct strings
- Find all distinct palindromic sub-strings of a given string
- Sub-strings of length K containing same character

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.