
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all distinct circular strings of length M in lexicographical order in C++
In this problem, we are given a string and an integer M. our task is to print all distinct circular strings of length M in lexicographical order (alphabetical order).
Let’s take an example to understand the problem,
Input: str= “ssssn” M=3 Output: nss sns ssn sss
Explanation − all possible circular strings of length 3 are: sss sss ssn sns nss. Distinct elements in lexicographical order are sss ssn sns nss.
To solve this problem, we will iterate over the elements of the string and generate all possible substrings of length M. we will store this generated string in a set that stores distinct elements only and discards duplicates. It will store these elements in a lexicographical order. Print all elements of the set from the beginning.
This algorithm will depend on both the size of the substring and the length of a string. Time complexity = O(N*M).
Example
This code shows the implementation of our solution −
#include <bits/stdc++.h> using namespace std; void printCircularString(string s, int l, int m) { set<string> circularString; s = s + s; for (int i = 0; i < l; i++) { circularString.insert(s.substr(i, m)); } while (!circularString.empty()) { cout<<*circularString.begin()<<"\t"; circularString.erase(circularString.begin()); } } int main() { string str = "ssssn"; int N = str.length(); int M = 3; cout<<"All circular strings of length "<<M<<" from the string '"<<str<<"' are:\n"; printCircularString(str, N, M); return 0; }
Output
All circular strings of length 3 from the string 'ssssn' are − nss sns ssn sss
- Related Articles
- Print all the combinations of a string in lexicographical order in C++
- Print all longest common sub-sequences in lexicographical order in C++
- The k-th Lexicographical String of All Happy Strings of Length n in C++
- Print all distinct characters of a string in order in C++
- Order strings by length of characters IN mYsql?
- Print common characters of two Strings in alphabetical order in C++
- Last Substring in Lexicographical Order in C++
- Print all interleavings of given two strings in C++
- Print all sequences of given length in C++
- Print All Distinct Elements of a given integer array in C++
- How to Sort Elements in Lexicographical Order (Dictionary Order) in Golang?
- Sort the words in lexicographical order in Java
- Sort the words in lexicographical order in C#
- Sort the words in lexicographical order in Python
- K-th Smallest in Lexicographical Order in C++
