Skip to content
Related Articles
Print all distinct circular strings of length M in lexicographical order
• Last Updated : 09 Jun, 2021

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++

 `// C++ program to print all``// distinct circular strings``// of length M in lexicographical order``#include ``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 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;``}`

## Java

 `// Java program to print all``// distinct circular strings``// of length M in lexicographical order``import` `java.util.*;` `class` `GFG``{` `// Function to print all the distinct substrings``// in lexicographical order``static` `void` `printStrings(String s, ``int` `l, ``int` `m)``{``    ``// stores all the distinct substrings``    ``Set c = ``new` `LinkedHashSet<>();` `    ``// 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.add(s.substring(i, i+m));``    ``}` `    ``// prints all the distinct circular``    ``// substrings of length m``    ``Iterator itr = c.iterator();``    ``while` `(itr.hasNext())``    ``{` `        ``// Prints the substring``        ``String a =(String) itr.next();``        ``System.out.print(a+``" "``);``        ` `    ``}``    ``c.clear();``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``String str = ``"saurav"``;``    ``int` `N = str.length();``    ``int` `M = ``4``;` `    ``printStrings(str, N, M);``}``}` `// This code contributed by Rajput-Ji`

## Python3

 `# Python program to print all``# distinct circular strings``# of length M in lexicographical order` `# Function to print all the distinct substrings``# in lexicographical order``def` `printStrings(s, l, m):` `    ``# stores all the distinct substrings``    ``c ``=` `set``()` `    ``# Append the string to self``    ``s ``=` `s``+``s` `    ``# Iterate over the length to generate``    ``# all substrings of length m``    ``for` `i ``in` `range``(l):` `        ``# insert the substring of length m``        ``# in the set``        ``c.add(s[i:i``+``m])` `    ``# prints all the distinct circular``    ``# substrings of length m``    ``for` `i ``in` `c:` `        ``# Prints the substring``        ``print``(i, end``=``" "``)`  `# Driver code``if` `__name__ ``=``=` `"__main__"``:` `    ``string ``=` `"saurav"``    ``N ``=` `len``(string)``    ``M ``=` `4` `    ``printStrings(string, N, M)` `# This code is contributed by``# sanjeev2552`

## C#

 `// C# program to print all``// distinct circular strings``// of length M in lexicographical order``using` `System;``using` `System.Collections.Generic;` `class` `GFG``{``    ``// Function to print all the distinct substrings``    ``// in lexicographical order``    ``static` `void` `printStrings(String s, ``int` `l, ``int` `m)``    ``{``        ``// stores all the distinct substrings``        ``HashSet<``string``> c = ``new` `HashSet<``string``>();` `        ``// 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.Add(s.Substring(i, m));``        ``}` `        ``// prints all the distinct circular``        ``// substrings of length m``        ``foreach` `(``string` `i ``in` `c)``        ``{``            ``string` `a = (``string``)i;``            ``Console.Write(a + ``" "``);``        ``}``        ``c.Clear();``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``String str = ``"saurav"``;``        ``int` `N = str.Length;``        ``int` `M = 4;` `        ``printStrings(str, N, M);``    ``}``}` `// This code contributed by``// sanjeev2552`

## Javascript

 ``
Output:
`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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up