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 = 4
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 elements 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++
#include <bits/stdc++.h>
using namespace std;
void printStrings(string s, int l, int m)
{
set<string> c;
s = s + s;
for ( int i = 0; i < l; i++) {
c.insert(s.substr(i, m));
}
while (!c.empty()) {
cout << *c.begin() << " " ;
c.erase(c.begin());
}
}
int main()
{
string str = "saurav" ;
int N = str.length();
int M = 4;
printStrings(str, N, M);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void printStrings(String s, int l, int m)
{
Set<String> c = new LinkedHashSet<>();
s = s + s;
for ( int i = 0 ; i < l; i++)
{
c.add(s.substring(i, i+m));
}
Iterator itr = c.iterator();
while (itr.hasNext())
{
String a =(String) itr.next();
System.out.print(a+ " " );
}
c.clear();
}
public static void main(String[] args)
{
String str = "saurav" ;
int N = str.length();
int M = 4 ;
printStrings(str, N, M);
}
}
|
Python3
def printStrings(s, l, m):
c = set ()
s = s + s
for i in range (l):
c.add(s[i:i + m])
for i in c:
print (i, end = " " )
if __name__ = = "__main__" :
string = "saurav"
N = len (string)
M = 4
printStrings(string, N, M)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void printStrings(String s, int l, int m)
{
HashSet< string > c = new HashSet< string >();
s = s + s;
for ( int i = 0; i < l; i++)
{
c.Add(s.Substring(i, m));
}
foreach ( string i in c)
{
string a = ( string )i;
Console.Write(a + " " );
}
c.Clear();
}
public static void Main(String[] args)
{
String str = "saurav" ;
int N = str.Length;
int M = 4;
printStrings(str, N, M);
}
}
|
Javascript
<script>
function printStrings(s, l, m)
{
var c = new Set();
s = s + s;
for ( var i = 0; i < l; i++)
{
c.add(s.substring(i, i + m));
}
while (c.size != 0)
{
var tmp = [...c].sort()[0];
document.write( tmp + " " );
c. delete (tmp);
}
}
var str = "saurav" ;
var N = str.length;
var M = 4;
printStrings(str, N, M);
</script>
|
Output
aura avsa ravs saur urav vsau
Time Complexity: O(N*M), where N is the length of the string.
Auxiliary Space: O(N*M)
Last Updated :
30 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...