Dada una string S, la tarea es contar las distintas permutaciones de cada longitud posible de la string dada.
Nota: No se permite la repetición de caracteres en la string.
Entrada: S = “abc”
Salida: 15
Explicación:
Las permutaciones posibles de cada longitud son:
{“a”, “b”, “c”, “ab”, “bc”, “ac”, “ba”, “ca ”, “cb”, “abc”, “acb”, “bac”, “bca”, “taxi”, “cba”}Entrada: S = “xz”
Salida: 4
Enfoque: La idea es encontrar el número de combinaciones de todas las longitudes posibles de la string y su suma es el número total de permutaciones distintas posibles de diferentes longitudes. Por lo tanto, para una string de longitud N, el número total de permutaciones distintas de diferente longitud es:
Combinaciones totales posibles: n P 1 + n P 2 + n P 3 + n P 4 + …… + n P n
A continuación se muestra la implementación del enfoque anterior:
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>
4
Publicación traducida automáticamente
Artículo escrito por atulya_bajpai y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA