Wednesday, May 8, 2024
HomeData ModellingData Structure & AlgorithmCheck if a string is the typed name of the given name

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

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 = “neveropen”, 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)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments