Given an integer k and a string str consisting of lowercase English alphabets, the task is to count how many k-character words (with or without meaning) can be formed from the characters of str when repetition is not allowed.
Examples:
Input: str = “cat”, k = 3
Output: 6
Required words are “cat”, “cta”, “act”, “atc”, “tca” and “tac”.
Input: str = “geeksforgeeks”, k = 3
Output: 840
Approach: Count the number of distinct characters in str and store it in cnt, now the task is to arrange k characters out of cnt characters i.e. nPr = n! / (n – r)!.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findPermutation(string str, int k)
{
bool has[26] = { false };
int cnt = 0;
for ( int i = 0; i < str.length(); i++) {
if (!has[str[i] - 'a' ]) {
cnt++;
has[str[i] - 'a' ] = true ;
}
}
long long int ans = 1;
for ( int i = 2; i <= cnt; i++)
ans *= i;
for ( int i = cnt - k; i > 1; i--)
ans /= i;
return ans;
}
int main()
{
string str = "geeksforgeeks" ;
int k = 4;
cout << findPermutation(str, k);
return 0;
}
|
Java
import java.util.*;
class solution
{
static int findPermutation(String str, int k)
{
boolean [] has = new boolean [ 26 ];
Arrays.fill(has, false );
int cnt = 0 ;
for ( int i = 0 ; i < str.length(); i++) {
if (!has[str.charAt(i) - 'a' ])
{
cnt++;
has[str.charAt(i) - 'a' ] = true ;
}
}
int ans = 1 ;
for ( int i = 2 ; i <= cnt; i++)
ans *= i;
for ( int i = cnt - k; i > 1 ; i--)
ans /= i;
return ans;
}
public static void main(String args[])
{
String str = "geeksforgeeks" ;
int k = 4 ;
System.out.println(findPermutation(str, k));
}
}
|
Python3
import math as mt
def findPermutation(string, k):
has = [ False for i in range ( 26 )]
cnt = 0
for i in range ( len (string)):
if (has[ ord (string[i]) - ord ( 'a' )] = = False ):
cnt + = 1
has[ ord (string[i]) - ord ( 'a' )] = True
ans = 1
for i in range ( 2 , cnt + 1 ):
ans * = i
for i in range (cnt - k, 1 , - 1 ):
ans / / = i
return ans
string = "geeksforgeeks"
k = 4
print (findPermutation(string, k))
|
C#
using System;
class solution
{
static int findPermutation( string str, int k)
{
bool []has = new bool [26];
for ( int i = 0; i < 26 ; i++)
has[i] = false ;
int cnt = 0;
for ( int i = 0; i < str.Length; i++) {
if (!has[str[i] - 'a' ])
{
cnt++;
has[str[i] - 'a' ] = true ;
}
}
int ans = 1;
for ( int i = 2; i <= cnt; i++)
ans *= i;
for ( int i = cnt - k; i > 1; i--)
ans /= i;
return ans;
}
public static void Main()
{
string str = "geeksforgeeks" ;
int k = 4;
Console.WriteLine(findPermutation(str, k));
}
}
|
PHP
<?php
function findPermutation( $str , $k )
{
$has = array ();
for ( $i = 0; $i < 26; $i ++)
{
$has [ $i ]= false;
}
$cnt = 0;
for ( $i = 0; $i < strlen ( $str ); $i ++)
{
if ( $has [ord( $str [ $i ]) - ord( 'a' )] == false)
{
$cnt ++;
$has [ord( $str [ $i ]) - ord( 'a' )] = true;
}
}
$ans = 1;
for ( $i = 2; $i <= $cnt ; $i ++)
$ans *= $i ;
for ( $i = $cnt - $k ; $i > 1; $i --)
$ans /= $i ;
return $ans ;
}
$str = "geeksforgeeks" ;
$k = 4;
echo findPermutation( $str , $k );
?>
|
Javascript
<script>
function findPermutation(str, k) {
var has = new Array(26);
for ( var i = 0; i < 26; i++) has[i] = false ;
var cnt = 0;
for ( var i = 0; i < str.length; i++) {
if (!has[str[i].charCodeAt(0) - "a" .charCodeAt(0)]) {
cnt++;
has[str[i].charCodeAt(0) - "a" .charCodeAt(0)] = true ;
}
}
var ans = 1;
for ( var i = 2; i <= cnt; i++) ans *= i;
for ( var i = cnt - k; i > 1; i--) ans /= i;
return ans;
}
var str = "geeksforgeeks" ;
var k = 4;
document.write(findPermutation(str, k));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1)