Check if a string is the typed name of the given name - GeeksforGeeks

29 May, 2021
Given a name and a typed-name of a person. Sometimes, when typing a vowel [aeiou], the key might get long pressed, and the character will be typed 1 or more times. The task is to examine the typed-name and tell if it is possible that typed name was of person’s name, with some characters (possibly none) being long pressed. Return ‘True‘ if it is, else ‘False‘.
Note: name and typed-name are separated by space with no space in between individuals names. Each character of the name is unique.
Examples:  
Input: str = “geeks”, typed = “geeeeks” Output: True The vowel ‘e’ repeats more times in typed and all other characters match.
Input: str = “alice”, typed = “aallicce” Output: False Here ‘l’ and ‘c’ are repeated which not a vowel. Hence name and typed-name represents different names.
Input: str = “alex”, typed = “aaalaeex” Output: False A vowel ‘a’ is extra in typed.  
Approach: The idea is based on Run Length Encoding. We consider only vowels and count their consecutive occurrences in str and typed. The count of occurrences in str must be less.
Below is the implementation of above approach. 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to implement run length encoding#include <bits/stdc++.h>using namespace std; // Check if the character is vowel or notbool 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 strbool 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 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 program to implement run# length encoding # Check if the character is# vowel or notdef 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 strdef 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 codename = "alex"typed = "aaalaeex"if (printRLE(name, typed)):    print("Yes")else:    print("No")     # This code is contributed# by Shashank_Sharma    
// C# program to implement run// length encodingusing System; class GFG{ // Check if the character is// vowel or notpublic 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 strpublic 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 Codepublic 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 program to implement// run length encoding // Check if the character is vowel or notfunction 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 strfunction 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    ?>
<script> // Javascript program to implement// run length encoding // Check if the character is vowel or notfunction 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 strfunction 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 codelet name = "alex", typed = "aaalaeex";if (printRLE(name, typed)){    document.write("Yes");}else{    document.write("No");}     // This code is contributed by decode2207 </script>
No
 
Time Complexity : O(m + n) Auxiliary Space : O(1) 
ankthon
Shivi_Aggarwal
Shashank_Sharma
shrikanth13
decode2207
simmytarika5
Pattern Searching
Strings
Strings
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Minimize number of cuts required to break N length stick into N unit length sticks
Check whether two strings contain same characters in same order
How to validate HTML tag using Regular Expression
How to validate GUID (Globally Unique Identifier) using Regular Expression
How to check Aadhaar number is valid or not using Regular Expression
Write a program to reverse an array or string
Reverse a string in Java
Longest Common Subsequence | DP-4
Write a program to print all permutations of a given string
C++ Data Types