Open In App

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

Last Updated : 06 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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>


Output

No

Complexity Analysis:

  • Time Complexity : O(m + n) 
  • Auxiliary Complexity: O(1)


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads