📜  来自给定字符集的单词的可能性

📅  最后修改于: 2022-05-13 01:57:07.007000             🧑  作者: Mango

来自给定字符集的单词的可能性

给定两个字符串's' 和 'q',检查 q 的所有字符是否都存在于 's' 中。
例子:

Example:
Input: s = "abctd"
       q = "cat"
Output: Yes
Explanation:
All characters of "cat" are
present in "abctd"
 
Input: s = dog
       hod
Output: No
Explanation:
Given query 'hod' hod has the letter
'h' which is not available in set 'dog', 
hence the output is no.

一个简单的解决方案是一个一个地尝试所有字符。找出它在“q”中出现的次数,然后在“s”中。 'q' 中出现的次数必须小于或等于 's' 中的相同。如果所有字符都满足此条件,则返回 true。否则返回假。
一个有效的解决方案是创建一个长度为 256(可能字符数)的频率数组并将其初始化为 0。然后我们计算 's' 中存在的每个元素的频率。在计算 's' 中的字符后,我们遍历 'q' 并检查每个字符的频率是否小于其在 's' 中的频率,方法是减少其在为 's' 构造的频率数组中的频率。
下面给出的是上述方法的实现

C++
// CPP program to check if a query string
// is present is given set.
#include 
using namespace std;
 
const int MAX_CHAR = 256;
 
bool isPresent(string s, string q)
{
    // Count occurrences of all characters
    // in s.
    int freq[MAX_CHAR] = { 0 };
    for (int i = 0; i < s.length(); i++)
        freq[s[i]]++;
 
    // Check if number of occurrences of
    // every character in q is less than
    // or equal to that in s.
    for (int i = 0; i < q.length(); i++) {
        freq[q[i]]--;
        if (freq[q[i]] < 0)
           return false;
    }
 
    return true;
}
 
// driver program
int main()
{
    string s = "abctd";
    string q = "cat";
 
    if (isPresent(s, q))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// java program to check if a query
// string is present is given set.
import java.io.*;
 
public class GFG {
 
    static int MAX_CHAR = 256;
     
    static boolean isPresent(String s, String q)
    {
         
        // Count occurrences of all
        // characters in s.
        int []freq = new int[MAX_CHAR];
        for (int i = 0; i < s.length(); i++)
            freq[s.charAt(i)]++;
     
        // Check if number of occurrences of
        // every character in q is less than
        // or equal to that in s.
        for (int i = 0; i < q.length(); i++)
        {
            freq[q.charAt(i)]--;
             
            if (freq[q.charAt(i)] < 0)
                return false;
        }
     
        return true;
    }
     
    // driver program
    static public void main (String[] args)
    {
        String s = "abctd";
        String q = "cat";
     
        if (isPresent(s, q))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by vt_m.


Python 3
# Python 3 program to check if a query
# string is present is given set.
MAX_CHAR = 256
 
def isPresent(s, q):
 
    # Count occurrences of all characters
    # in s.
    freq = [0] *  MAX_CHAR
    for i in range(0 , len(s)):
        freq[ord(s[i])] += 1
 
    # Check if number of occurrences of
    # every character in q is less than
    # or equal to that in s.
    for i in range(0, len(q)):
        freq[ord(q[i])] -= 1
        if (freq[ord(q[i])] < 0):
            return False
     
    return True
 
# driver program
s = "abctd"
q = "cat"
 
if (isPresent(s, q)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Smitha


C#
// C# program to check if a query
// string is present is given set.
using System;
 
public class GFG {
     
    static int MAX_CHAR = 256;
     
    static bool isPresent(string s, string q)
    {
 
        // Count occurrences of all
        // characters in s.
        int []freq = new int[MAX_CHAR];
         
        for (int i = 0; i < s.Length; i++)
            freq[s[i]]++;
     
        // Check if number of occurrences of
        // every character in q is less than
        // or equal to that in s.
        for (int i = 0; i < q.Length; i++)
        {
            freq[q[i]]--;
             
            if (freq[q[i]] < 0)
                return false;
        }
     
        return true;
    }
     
    // driver program
    static public void Main ()
    {
        string s = "abctd";
        string q = "cat";
     
        if (isPresent(s, q))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

Yes