Comprobar si una string es el nombre escrito del nombre de pila

Dado un nombre y un nombre escrito de una persona. A veces, al escribir una vocal [aeiou] , la tecla puede mantenerse presionada y el carácter se escribirá 1 o más veces. La tarea es examinar el nombre escrito y decir si es posible que el nombre escrito fuera el nombre de una persona, con algunos caracteres (posiblemente ninguno) presionados prolongadamente. Devuelve ‘ Verdadero ‘ si lo es, de lo contrario ‘ Falso ‘.

Nota: el nombre y el nombre escrito están separados por espacios sin espacios entre los nombres de las personas. Cada carácter del nombre es único .

Ejemplos:  

Entrada: str = “geeks”, typed = “geeeeks” 
Salida: True 
La vocal ‘e’ se repite más veces en typed y todos los demás caracteres coinciden.

Entrada: str = “alice”, typed = “aallicce” 
Salida: Falso 
Aquí se repiten ‘l’ y ‘c’ que no son vocales. 
Por lo tanto, name y typed-name representan nombres diferentes.

Entrada: str = “alex”, tipeado = “aaalaeex” 
Salida: Falso 
Una vocal ‘a’ es extra en tipeado. 
 

Enfoque: la idea se basa en la codificación de longitud de ejecución . Consideramos solo las vocales y contamos sus ocurrencias consecutivas en str y typed. El conteo de ocurrencias en str debe ser menor.

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

C++

// CPP program to implement run length encoding
#include <bits/stdc++.h>
using namespace std;
 
// Check if the character is vowel or not
bool isVowel(char c)
{
    string vowel = "aeiou";
    for (int i = 0; i < vowel.length(); ++i)
        if (vowel[i] == c)
            return true;
    return false;
}
 
// Returns true if 'typed' is a typed name
// given str
bool printRLE(string str, string typed)
{
    int n = str.length(), m = typed.length();
 
    // Traverse through all characters of str.
    int j = 0;
    for (int i = 0; i < n; i++) {
 
        // If current characters do not match
        if (str[i] != typed[j])
            return false;
 
        // If not vowel, simply move ahead in both
        if (isVowel(str[i]) == false) {
            j++;
            continue;
        }
 
        // Count occurrences of current vowel in str
        int count1 = 1;
        while (i < n - 1 && str[i] == str[i + 1]) {
            count1++;
            i++;
        }
 
        // Count occurrences of current vowel in
        // typed.
        int count2 = 1;
        while (j < m - 1 && typed[j] == str[i]) {
            count2++;
            j++;
        }
 
        if (count1 > count2)
            return false;
    }
 
    return true;
}
 
int main()
{
    string name = "alex", typed = "aaalaeex";
    if (printRLE(name, typed))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

Java

// Java program to implement run length encoding
 
public class Improve {
 
    // Check if the character is vowel or not
    static boolean isVowel(char c)
    {
        String vowel = "aeiou";
        for (int i = 0; i < vowel.length(); ++i)
            if (vowel.charAt(i) == c)
                return true;
        return false;
    }
 
    // Returns true if 'typed' is a typed name
    // given str
    static boolean printRLE(String str, String typed)
    {
        int n = str.length(), m = typed.length();
 
        // Traverse through all characters of str.
        int j = 0;
        for (int i = 0; i < n; i++) {
 
            // If current characters do not match
            if (str.charAt(i) != typed.charAt(j))
                return false;
 
            // If not vowel, simply move ahead in both
            if (isVowel(str.charAt(i)) == false) {
                j++;
                continue;
            }
 
            // Count occurrences of current vowel in str
            int count1 = 1;
            while (i < n - 1 && str.charAt(i) == str.charAt(i+1)) {
                count1++;
                i++;
            }
 
            // Count occurrences of current vowel in
            // typed.
            int count2 = 1;
            while (j < m - 1 && typed.charAt(j) == str.charAt(i)) {
                count2++;
                j++;
            }
 
            if (count1 > count2)
                return false;
        }
 
        return true;
    }
     
    public static void main(String args[])
    {
 
           String name = "alex", typed = "aaalaeex";
            if (printRLE(name, typed))
                System.out.println("Yes");
            else
                System.out.println("No");
           
    }
    // This code is contributed by ANKITRAI1
}

Python3

# Python3 program to implement run
# length encoding
 
# Check if the character is
# vowel or not
def isVowel(c):
     
    vowel = "aeiou"
    for i in range(len(vowel)):
        if(vowel[i] == c):
            return True
    return False
     
# Returns true if 'typed' is a
# typed name given str
def printRLE(str, typed):
     
    n = len(str)
    m = len(typed)
     
    # Traverse through all
    # characters of str
    j = 0
    for i in range(n):
         
        # If current characters do
        # not match
        if str[i] != typed[j]:
            return False
         
        # If not vowel, simply move
        # ahead in both
        if isVowel(str[i]) == False:
            j = j + 1
            continue
         
        # Count occurrences of current
        # vowel in str
        count1 = 1
        while (i < n - 1 and (str[i] == str[i + 1])):
            count1 = count1 + 1
            i = i + 1
             
        # Count occurrence of current
        # vowel in typed
        count2 = 1
        while(j < m - 1 and typed[j] == str[i]):
            count2 = count2 + 1
            j = j + 1
         
        if count1 > count2:
            return False
     
    return True
 
# Driver code
name = "alex"
typed = "aaalaeex"
if (printRLE(name, typed)):
    print("Yes")
else:
    print("No")
     
# This code is contributed
# by Shashank_Sharma    

C#

// C# program to implement run
// length encoding
using System;
 
class GFG
{
 
// Check if the character is
// vowel or not
public static bool isVowel(char c)
{
    string vowel = "aeiou";
    for (int i = 0;
             i < vowel.Length; ++i)
    {
        if (vowel[i] == c)
        {
            return true;
        }
    }
    return false;
}
 
// Returns true if 'typed' is
//  a typed name given str
public static bool printRLE(string str,
                            string typed)
{
    int n = str.Length, m = typed.Length;
 
    // Traverse through all
    // characters of str.
    int j = 0;
    for (int i = 0; i < n; i++)
    {
 
        // If current characters
        // do not match
        if (str[i] != typed[j])
        {
            return false;
        }
 
        // If not vowel, simply move
        // ahead in both
        if (isVowel(str[i]) == false)
        {
            j++;
            continue;
        }
 
        // Count occurrences of current
        // vowel in str
        int count1 = 1;
        while (i < n - 1 &&
               str[i] == str[i + 1])
        {
            count1++;
            i++;
        }
 
        // Count occurrences of current
        // vowel in typed
        int count2 = 1;
        while (j < m - 1 &&
               typed[j] == str[i])
        {
            count2++;
            j++;
        }
 
        if (count1 > count2)
        {
            return false;
        }
    }
 
    return true;
}
 
// Driver Code
public static void Main(string[] args)
{
    string name = "alex",
           typed = "aaalaeex";
    if (printRLE(name, typed))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed
// by Shrikant13

PHP

<?php
// PHP program to implement
// run length encoding
 
// Check if the character is vowel or not
function isVowel($c)
{
    $vowel = "aeiou";
    for ($i = 0; $i < strlen($vowel); ++$i)
        if ($vowel[$i] == $c)
            return true;
    return false;
}
 
// Returns true if 'typed'
// is a typed name
// given str
function printRLE($str, $typed)
{
    $n = strlen($str);
    $m = strlen($typed);
 
    // Traverse through all
    // characters of str.
    $j = 0;
    for ($i = 0; $i < $n; $i++) {
 
        // If current characters
        // do not match
        if ($str[$i] != $typed[$j])
            return false;
 
        // If not vowel, simply
        // move ahead in both
        if (isVowel($str[$i]) == false) {
            $j++;
            continue;
        }
 
        // Count occurrences of
        // current vowel in str
        $count1 = 1;
        while ($i < $n - 1 && $str[$i] == $str[$i + 1]) {
            $count1++;
            $i++;
        }
 
        // Count occurrences of
        // current vowel in typed.
        $count2 = 1;
        while ($j < $m - 1 && $typed[$j] == $str[$i]) {
            $count2++;
            $j++;
        }
 
        if ($count1 > $count2)
            return false;
    }
 
    return true;
}
 
// Driver code
$name = "alex";
$typed = "aaalaeex";
if (printRLE($name, $typed))
        echo "Yes";
else
        echo "No";
         
// This code is contributed
// by Shivi_Aggarwal    
?>

Javascript

<script>
 
// Javascript program to implement
// run length encoding
 
// Check if the character is vowel or not
function isVowel(c)
{
    let vowel = "aeiou";
    for(let i = 0; i < vowel.length; ++i)
    {
        if (vowel[i] == c)
        {
            return true;
        }
    }
    return false;
}
 
// Returns true if 'typed' is
//  a typed name given str
function printRLE(str, typed)
{
    let n = str.length, m = typed.length;
 
    // Traverse through all
    // characters of str.
    let j = 0;
    for(let i = 0; i < n; i++)
    {
         
        // If current characters
        // do not match
        if (str[i] != typed[j])
        {
            return false;
        }
 
        // If not vowel, simply move
        // ahead in both
        if (isVowel(str[i]) == false)
        {
            j++;
            continue;
        }
 
        // Count occurrences of current
        // vowel in str
        let count1 = 1;
        while (i < n - 1 &&
               str[i] == str[i + 1])
        {
            count1++;
            i++;
        }
 
        // Count occurrences of current
        // vowel in typed
        let count2 = 1;
        while (j < m - 1 &&
               typed[j] == str[i])
        {
            count2++;
            j++;
        }
        if (count1 > count2)
        {
            return false;
        }
    }
    return true;
}
 
// Driver code
let name = "alex", typed = "aaalaeex";
if (printRLE(name, typed))
{
    document.write("Yes");
}
else
{
    document.write("No");
}
     
// This code is contributed by decode2207
 
</script>
Producción: 

No

 

Complejidad de Tiempo : O(m + n) 
Espacio Auxiliar : O(1)
 

Publicación traducida automáticamente

Artículo escrito por Sanjit_Prasad 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 *