📌  相关文章
📜  使用给定的约束检查一个字符串可以由另一个字符串组成

📅  最后修改于: 2021-05-06 19:11:59             🧑  作者: Mango

给定两个字符串S1和S2(所有字符均小写)。任务是检查是否可以使用给定约束从S1形成S2:

1.如果S2中有两个“ a”,则S1中存在S2的字符,那么S1也应具有两个“ a”。
2.如果S1中不存在S2的任何字符,请检查S1中是否存在前两个ASCII字符。例如,如果S2中有“ e”,而S1中没有,则可以从S1中使用“ c”和“ d”来制作“ e”。

注意: S1中的所有字符只能使用一次。

例子:

方法:可以使用哈希解决以上问题。 S1中所有字符的计数都存储在哈希表中。遍历字符串,并检查哈希表中是否存在S2中的字符,请减少哈希表中该特定字符的计数。如果哈希表中没有该字符,请检查哈希表中是否存在前两个ASCII字符,然后减少哈希表中前两个ASCII字符的计数。如果可以使用给定的约束从S1形成所有字符,则可以从S1形成字符串S2,否则无法形成字符串。

下面是上述方法的实现:

C++
// CPP program to Check if a given
// string can be formed from another
// string using given constraints
#include 
using namespace std;
  
// Function to check if S2 can be formed of S1
bool check(string S1, string S2)
{
    // length of strings
    int n1 = S1.size();
    int n2 = S2.size();
  
    // hash-table to store count
    unordered_map mp;
  
    // store count of each character
    for (int i = 0; i < n1; i++) {
        mp[S1[i]]++;
    }
  
    // traverse and check for every character
    for (int i = 0; i < n2; i++) {
  
        // if the character of s2 is present in s1
        if (mp[S2[i]]) {
            mp[S2[i]]--;
        }
  
        // if the character of s2 is not present in
        // S1, then check if previous two ASCII characters
        // are present in S1
        else if (mp[S2[i] - 1] && mp[S2[i] - 2]) {
  
            mp[S2[i] - 1]--;
            mp[S2[i] - 2]--;
        }
        else {
            return false;
        }
    }
  
   return true;
}
  
// Driver Code
int main()
{
    string S1 = "abbat";
    string S2 = "cat";
  
    // Calling function to check
    if (check(S1, S2))
        cout << "YES";
    else
        cout << "NO";
}


Java
// JAVA program to Check if a given
// String can be formed from another
// String using given constraints
import java.util.*;
  
class GFG
{
  
// Function to check if S2 can be formed of S1
static boolean check(String S1, String S2)
{
    // length of Strings
    int n1 = S1.length();
    int n2 = S2.length();
  
    // hash-table to store count
    HashMap mp = 
        new HashMap();
  
    // store count of each character
    for (int i = 0; i < n1; i++)
    {
        if(mp.containsKey((int)S1.charAt(i)))
        {
            mp.put((int)S1.charAt(i), 
            mp.get((int)S1.charAt(i)) + 1);
        }
        else
        {
            mp.put((int)S1.charAt(i), 1);
        }
    }
  
    // traverse and check for every character
    for (int i = 0; i < n2; i++)
    {
  
        // if the character of s2 is present in s1
        if(mp.containsKey((int)S2.charAt(i))) 
        {
            mp.put((int)S2.charAt(i), 
            mp.get((int)S2.charAt(i)) - 1);
        }
  
        // if the character of s2 is not present in
        // S1, then check if previous two ASCII characters
        // are present in S1
        else if (mp.containsKey(S2.charAt(i)-1) && 
                    mp.containsKey(S2.charAt(i)-2))
        {
            mp.put((S2.charAt(i) - 1), 
            mp.get(S2.charAt(i) - 1) - 1);
            mp.put((S2.charAt(i) - 2), 
            mp.get(S2.charAt(i) - 2) - 1);
        }
        else 
        {
            return false;
        }
    }
  
    return true;
}
  
// Driver Code
public static void main(String[] args)
{
    String S1 = "abbat";
    String S2 = "cat";
  
    // Calling function to check
    if (check(S1, S2))
        System.out.print("YES");
    else
        System.out.print("NO");
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to Check if a given string 
# can be formed from another string using 
# given constraints 
from collections import defaultdict
  
# Function to check if S2 can 
# be formed of S1 
def check(S1, S2): 
  
    # length of strings 
    n1 = len(S1) 
    n2 = len(S2) 
  
    # hash-table to store count 
    mp = defaultdict(lambda:0) 
  
    # store count of each character 
    for i in range(0, n1): 
        mp[S1[i]] += 1
  
    # traverse and check for every character 
    for i in range(0, n2): 
  
        # if the character of s2 is 
        # present in s1 
        if mp[S2[i]]: 
            mp[S2[i]] -= 1
  
        # if the character of s2 is not present
        # in S1, then check if previous two ASCII 
        # characters are present in S1 
        elif (mp[chr(ord(S2[i]) - 1)] and 
              mp[chr(ord(S2[i]) - 2)]): 
  
            mp[chr(ord(S2[i]) - 1)] -= 1
            mp[chr(ord(S2[i]) - 2)] -= 1
          
        else:
            return False
  
    return True
  
# Driver Code 
if __name__ == "__main__":
  
    S1 = "abbat"
    S2 = "cat"
  
    # Calling function to check 
    if check(S1, S2): 
        print("YES") 
    else:
        print("NO") 
  
# This code is contributed by Rituraj Jain


C#
// C# program to Check if a given
// String can be formed from another
// String using given constraints
using System;
using System.Collections.Generic;
  
class GFG
{
  
// Function to check if S2 can be formed of S1
static bool check(String S1, String S2)
{
    // length of Strings
    int n1 = S1.Length;
    int n2 = S2.Length;
  
    // hash-table to store count
    Dictionary mp = 
        new Dictionary();
  
    // store count of each character
    for (int i = 0; i < n1; i++)
    {
        if(mp.ContainsKey((int)S1[i]))
        {
            mp[(int)S1[i]] = mp[(int)S1[i]] + 1;
        }
        else
        {
            mp.Add((int)S1[i], 1);
        }
    }
  
    // traverse and check for every character
    for (int i = 0; i < n2; i++)
    {
  
        // if the character of s2 is present in s1
        if(mp.ContainsKey((int)S2[i])) 
        {
            mp[(int)S2[i]] = mp[(int)S2[i]] - 1;
        }
  
        // if the character of s2 is not present in
        // S1, then check if previous two ASCII characters
        // are present in S1
        else if (mp.ContainsKey(S2[i] - 1) && 
                    mp.ContainsKey(S2[i] - 2))
        {
            mp[S2[i] - 1] = mp[S2[i] - 1] - 1;
            mp[S2[i] - 2] = mp[S2[i] - 2] - 1;
        }
        else
        {
            return false;
        }
    }
  
    return true;
}
  
// Driver Code
public static void Main(String[] args)
{
    String S1 = "abbat";
    String S2 = "cat";
  
    // Calling function to check
    if (check(S1, S2))
        Console.Write("YES");
    else
        Console.Write("NO");
}
}
  
// This code is contributed by PrinciRaj1992


输出:
YES
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”