Check if a string is the typed name of the given name
Last Updated :
06 Sep, 2022
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++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char c)
{
string vowel = "aeiou" ;
for ( int i = 0; i < vowel.length(); ++i)
if (vowel[i] == c)
return true ;
return false ;
}
bool printRLE(string str, string typed)
{
int n = str.length(), m = typed.length();
int j = 0;
for ( int i = 0; i < n; i++) {
if (str[i] != typed[j])
return false ;
if (isVowel(str[i]) == false ) {
j++;
continue ;
}
int count1 = 1;
while (i < n - 1 && str[i] == str[i + 1]) {
count1++;
i++;
}
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
public class Improve {
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 ;
}
static boolean printRLE(String str, String typed)
{
int n = str.length(), m = typed.length();
int j = 0 ;
for ( int i = 0 ; i < n; i++) {
if (str.charAt(i) != typed.charAt(j))
return false ;
if (isVowel(str.charAt(i)) == false ) {
j++;
continue ;
}
int count1 = 1 ;
while (i < n - 1 && str.charAt(i) == str.charAt(i+ 1 )) {
count1++;
i++;
}
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" );
}
}
|
Python3
def isVowel(c):
vowel = "aeiou"
for i in range ( len (vowel)):
if (vowel[i] = = c):
return True
return False
def printRLE( str , typed):
n = len ( str )
m = len (typed)
j = 0
for i in range (n):
if str [i] ! = typed[j]:
return False
if isVowel( str [i]) = = False :
j = j + 1
continue
count1 = 1
while (i < n - 1 and ( str [i] = = str [i + 1 ])):
count1 = count1 + 1
i = i + 1
count2 = 1
while (j < m - 1 and typed[j] = = str [i]):
count2 = count2 + 1
j = j + 1
if count1 > count2:
return False
return True
name = "alex"
typed = "aaalaeex"
if (printRLE(name, typed)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
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 ;
}
public static bool printRLE( string str,
string typed)
{
int n = str.Length, m = typed.Length;
int j = 0;
for ( int i = 0; i < n; i++)
{
if (str[i] != typed[j])
{
return false ;
}
if (isVowel(str[i]) == false )
{
j++;
continue ;
}
int count1 = 1;
while (i < n - 1 &&
str[i] == str[i + 1])
{
count1++;
i++;
}
int count2 = 1;
while (j < m - 1 &&
typed[j] == str[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))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
PHP
<?php
function isVowel( $c )
{
$vowel = "aeiou" ;
for ( $i = 0; $i < strlen ( $vowel ); ++ $i )
if ( $vowel [ $i ] == $c )
return true;
return false;
}
function printRLE( $str , $typed )
{
$n = strlen ( $str );
$m = strlen ( $typed );
$j = 0;
for ( $i = 0; $i < $n ; $i ++) {
if ( $str [ $i ] != $typed [ $j ])
return false;
if (isVowel( $str [ $i ]) == false) {
$j ++;
continue ;
}
$count1 = 1;
while ( $i < $n - 1 && $str [ $i ] == $str [ $i + 1]) {
$count1 ++;
$i ++;
}
$count2 = 1;
while ( $j < $m - 1 && $typed [ $j ] == $str [ $i ]) {
$count2 ++;
$j ++;
}
if ( $count1 > $count2 )
return false;
}
return true;
}
$name = "alex" ;
$typed = "aaalaeex" ;
if (printRLE( $name , $typed ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function isVowel(c)
{
let vowel = "aeiou" ;
for (let i = 0; i < vowel.length; ++i)
{
if (vowel[i] == c)
{
return true ;
}
}
return false ;
}
function printRLE(str, typed)
{
let n = str.length, m = typed.length;
let j = 0;
for (let i = 0; i < n; i++)
{
if (str[i] != typed[j])
{
return false ;
}
if (isVowel(str[i]) == false )
{
j++;
continue ;
}
let count1 = 1;
while (i < n - 1 &&
str[i] == str[i + 1])
{
count1++;
i++;
}
let count2 = 1;
while (j < m - 1 &&
typed[j] == str[i])
{
count2++;
j++;
}
if (count1 > count2)
{
return false ;
}
}
return true ;
}
let name = "alex" , typed = "aaalaeex" ;
if (printRLE(name, typed))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Complexity Analysis:
- Time Complexity : O(m + n)
- Auxiliary Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...