Dada una string y un entero M, imprima todas las strings circulares distintas de longitud M en orden lexicográfico.
Ejemplos:
Entrada: str = “baaaa”, M = 3
Salida: aaa aab aba baa
Todas las substrings circulares posibles de longitud 3 son “baa” “aaa” “aaa” “aab” “aba”
De las 6, 4 son distintas y el orden lexicografico es aaa aab aba baaEntrada: str = “saurav”, M = 4
Salida: aura avsa ravs saur urav vsau
Todas las substrings circulares posibles de longitud 4 son saur aura urav ravs avsa vsau.
Todas las substrings son distintas, el orden lexicográfico es aura avsa ravs saur urav vsau.
Enfoque: La función substr se utiliza para resolver el problema. Agregue la string a sí misma al principio. Iterar sobre la longitud de la string para generar todas las substrings posibles de longitud M. Set se usa en C++ para almacenar todas las substrings distintas de longitud 4, set almacena de forma predeterminada todos sus elementos en orden lexicográfico. Una vez que se generan todas las strings, imprima los elementos del conjunto desde el principio.
A continuación se muestra la implementación del enfoque anterior:
C++
// 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; }
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<String> 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
<script> // Javascript program to print all // distinct circular strings // of length M in lexicographical order // Function to print all the distinct substrings // in lexicographical order function printStrings(s, l, m) { // Stores all the distinct substrings var c = new Set(); // Append the string to self s = s + s; // Iterate over the length to generate // all substrings of length m for(var 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 while (c.size != 0) { var tmp = [...c].sort()[0]; // Prints the substring document.write( tmp + " "); // Erases the beginning element after // printing c.delete(tmp); } } // Driver code var str = "saurav"; var N = str.length; var M = 4; printStrings(str, N, M); // This code is contributed by itsok </script>
aura avsa ravs saur urav vsau
Complejidad de tiempo : O(N*M), donde N es la longitud de la string.