
- 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 permutations of a given string with duplicates in C++
In this problem, we are given a string that may contain duplicate characters. Our task is to print all distinct permutations of the strings.
Let’s take an example to understand the problem −
Input: string = “XYZ” Output: XYZ XZY YXZ YZX ZYX ZXY
To solve this problem, we have to fix one element of the string. And then iterate all the elements of the strings.
Example
Program to implement our solution,
#include <string.h> #include <iostream> using namespace std; int compare(const void* a, const void* b) { return (*(char*)a - *(char*)b); } void swapChar(char* a, char* b) { char t = *a; *a = *b; *b = t; } int findCeil(char str[], char first, int l, int h) { int ceilIndex = l; for (int i = l + 1; i <= h; i++) if (str[i] > first && str[i] < str[ceilIndex]) ceilIndex = i; return ceilIndex; } void printPermutations(char str[]) { int size = strlen(str); qsort(str, size, sizeof(str[0]), compare); bool isFinished = false; while (!isFinished) { static int x = 1; cout<<str<<"\t"; int i; for (i = size - 2; i >= 0; --i) if (str[i] < str[i + 1]) break; if (i == -1) isFinished = true; else { int ceilIndex = findCeil(str, str[i], i + 1, size - 1); swapChar(&str[i], &str[ceilIndex]); qsort(str + i + 1, size - i - 1, sizeof(str[0]), compare); } } } int main() { char str[] = "SNGY"; cout<<"All permutations of the string"<<str<<" are :\n"; printPermutations(str); return 0; }
Output
All permutations of the stringSNGY are − GNSY GNYS GSNY GSYN GYNS GYSN NGSY NGYS NSGY NSYG NYGS NYSG SGNY SGYN SNGY SNYG SYGN SYNG YGNS YGSN YNGS YNSG YSGN YSNG
- Related Articles
- Print all permutations of a given string
- Print distinct sorted permutations with duplicates allowed in input in C++
- Python Program to print all permutations of a given string
- C Program to print all permutations of a given string
- Java Program to print distinct permutations of a string
- Print all permutations of a string in Java
- Print all palindrome permutations of a string in C++
- Print first n distinct permutations of string using itertools in Python
- Print all the palindromic permutations of given string in alphabetic order in C++
- Print all permutations with repetition of characters in C++
- Print all distinct characters of a string in order in C++
- Print All Distinct Elements of a given integer array in C++
- Remove all duplicates from a given string in C#
- Remove all duplicates from a given string in Python
- How to find all possible permutations of a given string in Python?

Advertisements