Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Count of sub-strings of length n possible from the given string
Next article icon

Count of distinct permutations of every possible length of given string

Last Updated : 12 Oct, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a string S, the task is to count the distinct permutations of every possible length of the given string.

Note: Repetition of characters is not allowed in the string.

Input: S = “abc”
Output: 15
Explanation:
Possible Permutations of every length are:
{“a”, “b”, “c”, “ab”, “bc”, “ac”, “ba”, “ca”, “cb”, “abc”, “acb”, “bac”, “bca”, “cab”, “cba”}

Input: S = “xz”
Output: 4

Approach: The idea is to find the count of combinations of every possible length of the string and their sum is the total number of distinct permutations possible of different lengths. Therefore, for N length string total number of distinct permutation of different length is:

Total Combinations possible: nP1 + nP2 + nP3 + nP4 + …… + nPn

Below is the implementation of the above approach:

C++




// C++ implementation of the
// above approach
 
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
 
// Function to find the factorial
// of a number
int fact(int a)
{
    int i, f = 1;
 
    // Loop to find the factorial
    // of the given number
    for (i = 2; i <= a; i++)
        f = f * i;
    return f;
}
 
// Function to find the number
// of permutations possible
// for a given string
int permute(int n, int r)
{
    int ans = 0;
    ans = (fact(n) / fact(n - r));
    return ans;
}
 
// Function to find the total
// number of combinations possible
int findPermutations(int n)
{
    int sum = 0, P;
    for (int r = 1; r <= n; r++) {
        P = permute(n, r);
        sum = sum + P;
    }
    return sum;
}
 
// Driver Code
int main()
{
    string str = "xz";
    int result, n;
    n = str.length();
 
    cout << findPermutations(n);
    return 0;
}
 
 

Java




// Java implementation of the
// above approach
class GFG{
 
// Function to find the factorial
// of a number
static int fact(int a)
{
    int i, f = 1;
 
    // Loop to find the factorial
    // of the given number
    for(i = 2; i <= a; i++)
        f = f * i;
     
    return f;
}
 
// Function to find the number
// of permutations possible
// for a given String
static int permute(int n, int r)
{
    int ans = 0;
    ans = (fact(n) / fact(n - r));
    return ans;
}
 
// Function to find the total
// number of combinations possible
static int findPermutations(int n)
{
    int sum = 0, P;
    for(int r = 1; r <= n; r++)
    {
        P = permute(n, r);
        sum = sum + P;
    }
    return sum;
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "xz";
    int result, n;
    n = str.length();
 
    System.out.print(findPermutations(n));
}
}
 
// This code is contributed by Amit Katiyar
 
 

Python3




# Python3 program to implement
# the above approach
 
# Function to find the factorial
# of a number
def fact(a):
 
    f = 1
 
    # Loop to find the factorial
    # of the given number
    for i in range(2, a + 1):
        f = f * i
 
    return f
 
# Function to find the number
# of permutations possible
# for a given string
def permute(n, r):
 
    ans = 0
    ans = fact(n) // fact(n - r)
 
    return ans
 
# Function to find the total
# number of combinations possible
def findPermutations(n):
 
    sum = 0
    for r in range(1, n + 1):
        P = permute(n, r)
        sum = sum + P
 
    return sum
 
# Driver Code
str = "xz"
n = len(str)
 
# Function call
print(findPermutations(n))
 
# This code is contributed by Shivam Singh
 
 

C#




// C# implementation of the
// above approach
using System;
 
class GFG{
 
// Function to find the factorial
// of a number
static int fact(int a)
{
    int i, f = 1;
 
    // Loop to find the factorial
    // of the given number
    for(i = 2; i <= a; i++)
        f = f * i;
     
    return f;
}
 
// Function to find the number
// of permutations possible
// for a given String
static int permute(int n, int r)
{
    int ans = 0;
    ans = (fact(n) / fact(n - r));
    return ans;
}
 
// Function to find the total
// number of combinations possible
static int findPermutations(int n)
{
    int sum = 0, P;
    for(int r = 1; r <= n; r++)
    {
        P = permute(n, r);
        sum = sum + P;
    }
    return sum;
}
 
// Driver Code
public static void Main(String[] args)
{
    String str = "xz";
    int n;
    n = str.Length;
 
    Console.Write(findPermutations(n));
}
}
 
// This code is contributed by amal kumar choubey
 
 

Javascript




<script>
 
// Javascript implementation of the
// above approach
 
// Function to find the factorial
// of a number
function fact(a)
{
    var i, f = 1;
 
    // Loop to find the factorial
    // of the given number
    for (i = 2; i <= a; i++)
        f = f * i;
    return f;
}
 
// Function to find the number
// of permutations possible
// for a given string
function permute(n, r)
{
    var ans = 0;
    ans = (fact(n) / fact(n - r));
    return ans;
}
 
// Function to find the total
// number of combinations possible
function findPermutations(n)
{
    var sum = 0, P;
    for (var r = 1; r <= n; r++) {
        P = permute(n, r);
        sum = sum + P;
    }
    return sum;
}
 
// Driver Code
var str = "xz";
var result, n;
n = str.length;
document.write( findPermutations(n));
 
</script>
 
 
Output: 
4

 

Time Complexity: O(n2)
Auxiliary Space: O(1)



Next Article
Count of sub-strings of length n possible from the given string
author
atulya_bajpai
Improve
Article Tags :
  • C++
  • C++ Programs
  • Combinatorial
  • Competitive Programming
  • Computer Organization & Architecture
  • DSA
  • Experienced
  • Experiences
  • Internship
  • Mathematical
  • School Programming
  • Strings
  • permutation
Practice Tags :
  • CPP
  • Combinatorial
  • Mathematical
  • permutation
  • Strings

Similar Reads

  • Iterative program to generate distinct Permutations of a String
    Given a string str, the task is to generate all the distinct permutations of the given string iteratively. Examples: Input: str = "bba" Output: abb bab bba Input: str = "abc" Output: abc acb bac bca cab cba Approach: The number of permutations for a string of length n are n!. The following algorithm
    15+ min read
  • Count of sub-strings of length n possible from the given string
    Given a string str and an integer N, the task is to find the number of possible sub-strings of length N.Examples: Input: str = "geeksforgeeks", n = 5 Output: 9 All possible sub-strings of length 5 are "geeks", "eeksf", "eksfo", "ksfor", "sforg", "forge", "orgee", "rgeek" and "geeks".Input: str = "jg
    6 min read
  • Check whether it is possible to permute string such that it does not contain a palindrome of length 2
    Given a strings S length N consisting of only 'a', 'b' and 'c'. The task is to check if it is possible to permute the characters of S such that it will not contain a palindrome of length 2 or more as a substring. Examples: Input: S = "abac" Output: Yes Explanation : 1. The string contains a palindro
    5 min read
  • Find the number of strings formed using distinct characters of a given string
    Given a string str consisting of lowercase English alphabets, the task is to find the count of all possible string of maximum length that can be formed using the characters of str such that no two characters in the generated string are same.Examples: Input: str = "aba" Output: 2 "ab" and "ba" are th
    4 min read
  • Lexicographically smallest permutation of a string that contains all substrings of another string
    Given two strings A and B, the task is to find lexicographically the smallest permutation of string B such that it contains every substring from the string A as its substring. Print “-1” if no such valid arrangement is possible.Examples: Input: A = "aa", B = "ababab" Output: aaabbb Explanation: All
    10 min read
  • Number of distinct words of size N with at most K contiguous vowels
    Given two integers n and k, count the number of distinct strings of length n of lowercase alphabets such that there are at most k consecutive vowels in any part of the string.Note: Since the result can be very large, output the answer modulo 1e9 + 7. Examples: Input: n = 1, k = 0Output: 21Explanatio
    15 min read
  • Count of strings possible by replacing two consecutive same character with new character
    Given string str. The task is to count the number of all different strings possible if two consecutive same characters of the string can be replaced by one different character. Examples Input: str = "abclll" Output: 3 Explanation: There can be 3 different string including the original string as show
    8 min read
  • Count unique subsequences of length K
    Given an array of N numbers and an integer K. The task is to print the number of unique subsequences possible of length K. Examples: Input : a[] = {1, 2, 3, 4}, k = 3 Output : 4. Unique Subsequences are: {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4} Input: a[] = {1, 1, 1, 2, 2, 2 }, k = 3 Output : 4 Un
    11 min read
  • Count of strings that become equal to one of the two strings after one removal
    Given two strings str1 and str2, the task is to count all the valid strings. An example of a valid string is given below: If str1 = "toy" and str2 = "try". Then S = "tory" is a valid string because when a single character is removed from it i.e. S = "tory" = "try" it becomes equal to str1. This prop
    9 min read
  • Count unique substrings of a string S present in a wraparound string
    Given a string S which is an infinite wraparound string of the string "abcdefghijklmnopqrstuvwxyz", the task is to count the number of unique non-empty substrings of a string p are present in s. Examples: Input: S = "zab"Output: 6Explanation: All possible substrings are "z", "a", "b", "za", "ab", "z
    12 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences