Arreglo de palabras sin cambiar la posición relativa de vocal y consonantes

Dada una palabra de longitud inferior a 10, la tarea es encontrar varias formas en las que pueda organizarse sin cambiar la posición relativa de las vocales y las consonantes.
Ejemplos: 
 

Input: "GEEKS"
Output: 6

Input: "COMPUTER"
Output: 720

Acercarse:
 

  1. Contar las vocales y consonantes en la palabra
  2. Ahora encuentre el número total de formas de organizar solo las vocales
  3. Luego busque formas de organizar solo consonantes.
  4. Multiplique ambas respuestas para obtener el total de formas = (cantidad de formas de organizar solo vocales)*(cantidad de formas de organizar solo consonantes)

A continuación se muestra la implementación del enfoque anterior:
 

C++

// C++ program for Arrangement of words
// without changing the relative position of
// vowel and consonants
#include <bits/stdc++.h>
using namespace std;
 
#define ll long int
 
// this function return n!
ll factorial(ll n)
{
    ll res = 1;
    for (int i = 1; i <= n; i++)
        res = res * i;
 
    return res;
}
 
// this will return total number of ways
ll count(string word)
{
 
    // freq maintains frequency
    // of each character in word
    ll freq[27] = { 0 };
 
    ll vowel = 0, consonant = 0;
    for (int i = 0; i < word.length(); i++) {
        freq[word[i] - 'A']++;
 
        // check character is vowel or not
        if (word[i] == 'A' || word[i] == 'E'
            || word[i] == 'I'
            || word[i] == 'O' || word[i] == 'U') {
            vowel++;
        }
 
        // the characters that are not vowel
        // must be consonant
        else
            consonant++;
    }
 
    // number of ways to arrange vowel
    ll vowelArrange;
    vowelArrange = factorial(vowel);
    vowelArrange /= factorial(freq[0]);
    vowelArrange /= factorial(freq[4]);
    vowelArrange /= factorial(freq[8]);
    vowelArrange /= factorial(freq[14]);
    vowelArrange /= factorial(freq[20]);
 
    ll consonantArrange;
    consonantArrange = factorial(consonant);
    for (int i = 0; i < 26; i++) {
        if (i != 0 && i != 4 && i != 8 && i != 14 && i != 20)
            consonantArrange /= factorial(freq[i]);
    }
 
    // multiply both as these are independent
    ll total = vowelArrange * consonantArrange;
    return total;
}
 
// Driver function
int main()
{
    // string contains only
    // capital letters
    string word = "COMPUTER";
 
    // this will contain ans
    ll ans = count(word);
    cout << ans << endl;
    return 0;
}

Java

// Java program for Arrangement of words
// without changing the relative position of
// vowel and consonants
 
class GFG
{
     
    // this function return n!
    static long factorial(long n)
    {
        long res = 1;
        for (int i = 1; i <= n; i++)
            res = res * i;
     
        return res;
    }
     
    // this will return total number of ways
    static long count(String word)
    {
     
        // freq maintains frequency
        // of each character in word
        int freq[] =new int[27];
         
        for(int i=0;i<27;i++)
            freq[i]=0;
     
        long vowel = 0, consonant = 0;
        for (int i = 0; i < word.length(); i++) {
            freq[word.charAt(i) - 'A']++;
     
            // check character is vowel or not
            if (word.charAt(i) == 'A' || word.charAt(i) == 'E'
                || word.charAt(i) == 'I'
                || word.charAt(i) == 'O' || word.charAt(i) == 'U') {
                vowel++;
            }
     
            // the characters that are not vowel
            // must be consonant
            else
                consonant++;
        }
     
        // number of ways to arrange vowel
        long vowelArrange;
        vowelArrange = factorial(vowel);
        vowelArrange /= factorial(freq[0]);
        vowelArrange /= factorial(freq[4]);
        vowelArrange /= factorial(freq[8]);
        vowelArrange /= factorial(freq[14]);
        vowelArrange /= factorial(freq[20]);
     
        long consonantArrange;
        consonantArrange = factorial(consonant);
        for (int i = 0; i < 26; i++) {
            if (i != 0 && i != 4 && i != 8 && i != 14 && i != 20)
                consonantArrange /= factorial(freq[i]);
        }
     
        // multiply both as these are independent
        long total = vowelArrange * consonantArrange;
        return total;
    }
     
    // Driver function
    public static void main(String []args)
    {
        // string contains only
        // capital letters
        String word = "COMPUTER";
     
        // this will contain ans
        long ans = count(word);
        System.out.println(ans);
 
    }
 
}
 
// This code is contributed by ihritik

Python3

# Python3 program for Arrangement of words
# without changing the relative position of
# vowel and consonants
 
# this function return n!
def factorial(n):
    res = 1
    for i in range(1, n + 1):
        res = res * i
    return res
 
# this will return total number of ways
def count(word):
     
    # freq maintains frequency
    # of each character in word
    freq = [0 for i in range(30)]
    vowel = 0
    consonant = 0
    for i in range(len(word)):
        freq[ord(word[i]) -65 ] += 1
         
        # check character is vowel or not
        if(word[i] == 'A'or word[i] == 'E' or
           word[i] == 'I' or word[i] == 'O'or
           word[i] == 'U'):
            vowel += 1
 
        # the characters that are not
        # vowel must be consonant
        else:
            consonant += 1
 
    # number of ways to arrange vowel
    vowelArrange = factorial(vowel)
    vowelArrange //= factorial(freq[0])
    vowelArrange //= factorial(freq[4])
    vowelArrange //= factorial(freq[8])
    vowelArrange //= factorial(freq[14])
    vowelArrange //= factorial(freq[20])
 
    consonantArrange = factorial(consonant)
    for i in range(26):
        if(i != 0 and i != 4 and i != 8 and
           i != 14 and i != 20):
            consonantArrange//= factorial(freq[i])
 
    # multiply both as these are independent
    total = vowelArrange * consonantArrange
    return total
 
# Driver code
 
# string contains only
# capital letters
word = "COMPUTER"
 
# this will contain ans
ans = count(word)
print(ans)
 
# This code is contributed
# by sahilshelangia

C#

// C# program for Arrangement of words
// without changing the relative position of
// vowel and consonants
using System;
class GFG
{
     
    // this function return n!
    static long factorial(long n)
    {
        long res = 1;
        for (int i = 1; i <= n; i++)
            res = res * i;
     
        return res;
    }
     
    // this will return total number of ways
    static long count(string word)
    {
     
        // freq maintains frequency
        // of each character in word
        int []freq =new int[27];
         
        for(int i=0;i<27;i++)
            freq[i]=0;
     
        long vowel = 0, consonant = 0;
        for (int i = 0; i < word.Length; i++) {
            freq[word[i] - 'A']++;
     
            // check character is vowel or not
            if (word[i] == 'A' || word[i] == 'E'
                || word[i] == 'I'
                || word[i] == 'O' || word[i] == 'U') {
                vowel++;
            }
     
            // the characters that are not vowel
            // must be consonant
            else
                consonant++;
        }
     
        // number of ways to arrange vowel
        long vowelArrange;
        vowelArrange = factorial(vowel);
        vowelArrange /= factorial(freq[0]);
        vowelArrange /= factorial(freq[4]);
        vowelArrange /= factorial(freq[8]);
        vowelArrange /= factorial(freq[14]);
        vowelArrange /= factorial(freq[20]);
     
        long consonantArrange;
        consonantArrange = factorial(consonant);
        for (int i = 0; i < 26; i++) {
            if (i != 0 && i != 4 && i != 8 && i != 14 && i != 20)
                consonantArrange /= factorial(freq[i]);
        }
     
        // multiply both as these are independent
        long total = vowelArrange * consonantArrange;
        return total;
    }
     
    // Driver function
    public static void Main()
    {
        // string contains only
        // capital letters
        string word = "COMPUTER";
     
        // this will contain ans
        long ans = count(word);
        Console.WriteLine(ans);
 
    }
 
}
// This code is contributed by ihritik

PHP

<?php
// PHP program for Arrangement of words
// without changing the relative position
// of vowel and consonants
     
// this function return n!
function factorial($n)
{
    $res = 1;
    for ($i = 1; $i <= $n; $i++)
        $res = $res * $i;
 
    return $res;
}
 
// this will return total
// number of ways
function count1($word)
{
 
    // freq maintains frequency
    // of each character in word
    $freq = array_fill(0, 27, 0);
     
    for($i = 0; $i < 27; $i++)
        $freq[$i] = 0;
 
    $vowel = 0;
    $consonant = 0;
    for ($i = 0; $i < strlen($word); $i++)
    {
        $freq[ord($word[$i]) - 65]++;
 
        // check character is vowel or not
        if ($word[$i] == 'A' || $word[$i] == 'E' ||
            $word[$i] == 'I' || $word[$i] == 'O' ||
            $word[$i] == 'U')
        {
            $vowel++;
        }
 
        // the characters that are not
        // vowel must be consonant
        else
            $consonant++;
    }
 
    // number of ways to arrange vowel
    $vowelArrange = factorial($vowel);
    $vowelArrange /= factorial($freq[0]);
    $vowelArrange /= factorial($freq[4]);
    $vowelArrange /= factorial($freq[8]);
    $vowelArrange /= factorial($freq[14]);
    $vowelArrange /= factorial($freq[20]);
 
    $consonantArrange = factorial($consonant);
    for ($i = 0; $i < 26; $i++)
    {
        if ($i != 0 && $i != 4 && $i != 8 &&
                       $i != 14 && $i != 20)
            $consonantArrange /= factorial($freq[$i]);
    }
 
    // multiply both as these
    // are independent
    $total = $vowelArrange * $consonantArrange;
    return $total;
}
 
// Driver Code
 
// string contains only
// capital letters
$word = "COMPUTER";
 
// this will contain ans
$ans = count1($word);
echo ($ans);
 
// This code is contributed by mits
?>

Javascript

// Javascript program for Arrangement of words
// without changing the relative position
// of vowel and consonants
     
// this function return n!
function factorial(n)
{
    let res = 1;
    for (let i = 1; i <= n; i++)
        res = res * i;
 
    return res;
}
 
// this will return total
// number of ways
function count1(word)
{
 
    // freq maintains frequency
    // of each character in word
    let freq = new Array(27).fill(0);
     
    for(let i = 0; i < 27; i++)
        freq[i] = 0;
 
    let vowel = 0;
    let consonant = 0;
    for (let i = 0; i < word.length; i++)
    {
        freq[word.charCodeAt(i) - 65]++;
 
        // check character is vowel or not
        if (word[i] == 'A' || word[i] == 'E' ||
            word[i] == 'I' || word[i] == 'O' ||
            word[i] == 'U')
        {
            vowel++;
        }
 
        // the characters that are not
        // vowel must be consonant
        else
            consonant++;
    }
 
    // number of ways to arrange vowel
    vowelArrange = factorial(vowel);
    vowelArrange /= factorial(freq[0]);
    vowelArrange /= factorial(freq[4]);
    vowelArrange /= factorial(freq[8]);
    vowelArrange /= factorial(freq[14]);
    vowelArrange /= factorial(freq[20]);
 
    consonantArrange = factorial(consonant);
    for (let i = 0; i < 26; i++)
    {
        if (i != 0 && i != 4 && i != 8 &&
                    i != 14 && i != 20)
            consonantArrange /= factorial(freq[i]);
    }
 
    // multiply both as these
    // are independent
    let total = vowelArrange * consonantArrange;
    return total;
}
 
// Driver Code
 
// string contains only
// capital letters
let word = "COMPUTER";
 
// this will contain ans
let ans = count1(word);
document.write(ans);
 
// This code is contributed by gfgking
Producción: 

720

 

Complejidad de tiempo: O(n), donde n es el tamaño de la palabra de string dada
Espacio auxiliar: O(27)

Publicación traducida automáticamente

Artículo escrito por sahilshelangia y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *