
- 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 the combinations of a string in lexicographical order in C++
In this problem, we are given string str, and we have to print all the combinations of the characters in a lexicographical order.
Let’s take an example to understand the problem better −
Input: str = ‘XYZ’ Output : X XY XYZ XZ XZY Y YX YXZ YZ YZX Z ZX ZXY ZY ZYX
To solve this problem, we will print all the combinations of characters in the string. For this, we need a map data structure to store the characters of the string. For the implementation, we will need to use backtracking to keep track of all combinations.
Example
#include <bits/stdc++.h> using namespace std; void printResult(char* result, int len); void findstringCombination(char result[], char str[], int count[],int level, int size, int length) ; void printCharCombination(string str); int main(){ string str = "ABC"; cout<<"The combination of characters of the string :\n"; printCharCombination(str); return 0; } void findstringCombination(char result[], char str[], int count[],int level, int size, int length){ if (level == size) return; for (int i = 0; i < length; i++) { if (count[i] == 0) continue; count[i]--; result[level] = str[i]; printResult(result, level); findstringCombination(result, str, count, level + 1, size, length); count[i]++; } } void printCharCombination(string str){ map<char, int> mp; for (int i = 0; i < str.size(); i++) { if (mp.find(str[i]) != mp.end()) mp[str[i]] = mp[str[i]] + 1; else mp[str[i]] = 1; } char* input = new char[mp.size()]; int* count = new int[mp.size()]; char* result = new char[str.size()]; map<char, int>::iterator it = mp.begin(); int i = 0; for (it; it != mp.end(); it++) { input[i] = it->first; count[i] = it->second; i++; } int length = mp.size(); int size = str.size(); findstringCombination(result, input, count, 0, size, length); } void printResult(char* result, int len){ for (int i = 0; i <= len; i++) cout<<result[i]; cout<<endl; }
Output
The combination of characters of the string −
A AB ABC AC ACB B BA BAC BC BCA C CA CAB CB CBA
- Related Articles
- Print all longest common sub-sequences in lexicographical order in C++
- Print all distinct circular strings of length M in lexicographical order in C++
- Print all combinations of factors in C++
- Print all distinct characters of a string in order in C++
- Print all combinations of balanced parentheses in C++
- Print all the palindromic permutations of given string in alphabetic order in C++
- Python Program to Print All Permutations of a String in Lexicographic Order without Recursion
- Python Program to Print All Permutations of a String in Lexicographic Order using Recursion
- Print all combinations of points that can compose a given number in C++
- Sort the words in lexicographical order in Java
- Sort the words in lexicographical order in C#
- Sort the words in lexicographical order in Python
- The k-th Lexicographical String of All Happy Strings of Length n in C++
- Return a sorted array in lexicographical order in JavaScript
- Print all possible combinations of r elements in a given array of size n in C++

Advertisements