Print only sorted Permutation of a string

Last Updated :

Given a string s, the task is to print all permutations of a string that are in ascending order, i.e., their characters are in ascending order.

Examples:

Input: Cplusplus
Output: Cllppssuu
Out of all the permutations of the string Cllppssuu is the only string which is sorted. 

Input: cba 
Output: abc

RECOMMENDED: Characteristics or features of an Algorithm

Approach:
There will always be only one string that will be in ascending order.
Below is the implementation of the above approach.

C++

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;

void printString(string s)
{
    sort(s.begin(), s.end());
    cout << s << "\n";
}

int main()
{
    string s = "Cplusplus";
    printString(s);
    return 0;
}

Java

// Java implementation of the above approach
import java.util.Arrays;

public class Solution {
    public static void printString(String s) {
        char[] tempArray = s.toCharArray();
        
        // Sort tempArray
        Arrays.sort(tempArray);
        
        // Print the new sorted string
        System.out.println(tempArray);
    }

    public static void main(String[] args) {
        String s = "Cplusplus";
        printString(s);
    }
}

C#

// C# implementation of the above approach
using System;

class Solution {
    public static void printString(string s) {
        char[] arr = s.ToCharArray();
        Array.Sort(arr);
        Console.WriteLine(string.Join("", arr));
    }

    public static void Main() {
        string s = "Cplusplus";
        printString(s);
    }
}


Output:

Cllppssuu

Time Complexity: O(nlogn), due to sorting.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Comment
Next Article
Mithlesh Upadhyay Published 02 Oct, 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Similar Reads

  • Four Pillars of Object-Oriented Programming (OOPS)

    Get an overview of four pillars of object-oriented programming (OOP) and see their suitable real life examples. What is OOP? Picture a cluttered garage🛠️. Tools are scattered everywhere,…

    4 min read
  • Find the Nth Triacontakaihenagonal Number

    Given a number N, the task is to find Nth triacontakaihenagonal number. A triacontakaihenagonal number is class of figurate number. It has 31 – sided polygon called triacontakaihenagon.…

    1 min read
  • Check if a Number is a Tetracontagon Number

    Given an integer N, the task is to check if it is a Tetracontagon number or not. Tetracontagon number is class of figurate number. It has 40 –…

    2 min read
  • Find max rational number in given Array

    Given the Numerator and Denominator of N Rational Numbers, the task is to find the maximum Rational Number from the array. Examples: Input: N = 2 num[] =…

    2 min read
  • Check if a Number is Pentacontagon

    Given an integer N, the task is to check if it is a pentacontagon number or not. Pentacontagon number is class of figurate number. It has 50 –…

    2 min read
  • Check if a Number is Triacontakaidigon

    Given an integer N, the task is to check if it is a Triacontakaidigon number or not. Triacontakaidigon number is class of figurate number. It has 32 –…

    2 min read