📜  检查字符串是否是给定名称的类型化名称

📅  最后修改于: 2021-04-27 20:38:29             🧑  作者: Mango

给定一个人的名字和键入的名字。有时,在输入元音[aeiou]时,按键可能会被长按,并且字符会被键入1次或多次。任务是检查键入的名称,并判断键入的名称是否可能与人的名字相同,并且长按了某些字符(可能没有字符)。如果是,则返回“ True ”,否则返回“ False ”。

注意: name和typed-name用空格分隔,个人名称之间没有空格。名称的每个字符都是唯一的

例子:

方法:该思想基于游程长度编码。我们只考虑元音,并计算它们在str和type中的连续出现次数。 str中出现的次数必须更少。

下面是上述方法的实现。

C++
// CPP program to implement run length encoding
#include 
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 occurences of current 
        # vowel in str
        count1 = 1
        while (i < n - 1 and (str[i] == str[i + 1])):
            count1 = count1 + 1
            i = i + 1
              
        # Count occurence 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
 $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     
?>


输出:
No

时间复杂度: O(m + n)
辅助空间: O(1)