最新华为OD机试真题-单词大师(100分)

🍭 大家好这里是清隆学长 ,一枚热爱算法的程序员

✨ 本系列打算持续跟新华为OD-D卷的三语言AC题解

👏 感谢大家的订阅➕ 和 喜欢💗

📎在线评测链接

=> 单词大师(100分) <=

华为OD

🌍 评测功能需要 =>订阅专栏<= 后联系清隆解锁~

🍓OJ题目截图

alt

🥮 单词大师

问题描述

给定一个字符串数组 和一个字符串 。如果可以用 中的字母拼写出 中的某个单词,则认为你掌握了这个单词。 中的字符仅由小写字母 和特殊字符 ? 组成,其中 ? 可以代表任意一个字母。

注意:拼写时, 中的每个字母只能使用一次,? 也只能使用一次。

请输出你能够拼写出的 中的单词数量。如果一个也拼写不出,则输出

输入格式

第一行输入一个整数 ,表示数组 的长度。

接下来 行,每行输入一个字符串,表示 中的一个单词。

最后一行输入一个字符串

其中,

输出格式

输出一个整数,表示你能够拼写出的 中的单词数量。

样例输入

4
cat
bt
hat
tree
atach??

样例输出

3

样例输入

3
hello
world
cloud
welldonehoneyr

样例输出

2

样例输入

3
apple
car
window
welldoneapplec?

样例输出

2

数据范围

题解

这道题可以通过统计字符频率的方式来判断是否能拼写出每个单词。

  1. 首先统计 中每个字母出现的次数,以及 ? 出现的次数。
  2. 对于每个单词 ,统计其中每个字母出现的次数。
  3. 遍历单词的每个字母,如果该字母在 中出现的次数大于等于在 中出现的次数,则可以拼写;否则,如果 ? 的数量大于等于不足的字母数,也可以拼写;否则,无法拼写该单词。
  4. 如果能拼写该单词,则答案加一。
  5. 最后输出答案即可。

时间复杂度为 ,其中 为单词数量, 为单词的平均长度。空间复杂度为 ,因为只需要常数级的额外空间。

参考代码

  • Python
n = int(input())
words = []
for _ in range(n):
    words.append(input())
chars = input()

def can_spell(word, chars):
    cnt_word = [0] * 26
    for c in word:
        cnt_word[ord(c) - ord('a')] += 1
    
    cnt_chars = [0] * 26
    wild = 0
    for c in chars:
        if c == '?':
            wild += 1
        else:
            cnt_chars[ord(c) - ord('a')] += 1
    
    for i in range(26):
        if cnt_word[i] > cnt_chars[i]:
            if wild >= cnt_word[i] - cnt_chars[i]:
                wild -= cnt_word[i] - cnt_chars[i]
            else:
                return False
    
    return True

ans = 0
for word in words:
    if can_spell(word, chars):
        ans += 1

print(ans)
  • Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] words = new String[n];
        for (int i = 0; i < n; i++) {
            words[i] = sc.next();
        }
        String chars = sc.next();
        
        int ans = 0;
        for (String word : words) {
            if (canSpell(word, chars)) {
                ans++;
            }
        }
        
        System.out.println(ans);
    }
    
    private static boolean canSpell(String word, String chars) {
        int[] cntWord = new int[26];
        for (char c : word.toCharArray()) {
            cntWord[c - 'a']++;
        }
        
        int[] cntChars = new int[26];
        int wild = 0;
        for (char c : chars.toCharArray()) {
            if (c == '?') {
                wild++;
            } else {
                cntChars[c - 'a']++;
            }
        }
        
        for (int i = 0; i < 26; i++) {
            if (cntWord[i] > cntChars[i]) {
                if (wild >= cntWord[i] - cntChars[i]) {
                    wild -= cntWord[i] - cntChars[i];
                } else {
                    return false;
                }
            }
        }
        
        return true;
    }
}
  • Cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;

bool canSpell(string word, string chars) {
    vector<int> cntWord(26, 0);
    for (char c : word) {
        cntWord[c - 'a']++;
    }
    
    vector<int> cntChars(26, 0);
    int wild = 0;
    for (char c : chars) {
        if (c == '?') {
            wild++;
        } else {
            cntChars[c - 'a']++;
        }
    }
    
    for (int i = 0; i < 26; i++) {
        if (cntWord[i] > cntChars[i]) {
            if (wild >= cntWord[i] - cntChars[i]) {
                wild -= cntWord[i] - cntChars[i];
            } else {
                return false;
            }
        }
    }
    
    return true;
}

int main() {
    int n;
    cin >> n;
    vector<string> words(n);
    for (int i = 0; i < n; i++) {
        cin >> words[i];
    }
    string chars;
    cin >> chars;
    
    int ans = 0;
    for (string word : words) {
        if (canSpell(word, chars)) {
            ans++;
        }
    }
    
    cout << ans << endl;
    
    return 0;
}
#华为od##华为od题库##华为OD##华为OD机试算法题库##华为#
最新华为OD机试-D卷 文章被收录于专栏

本专栏给大家提供了华为2024最新华为OD-C/D卷的题目汇总和(Java/Cpp/Python)三语言解析 + 提供OJ在线评测

全部评论
🌍 评测功能需要 订阅专栏 后联系清隆解锁~ #华为od#
点赞
送花
回复 分享
发布于 07-02 19:05 浙江

相关推荐

仅执行一次字符串交换能否使两个字符串相等题目描述给你长度相等的两个字符串&nbsp;s1&nbsp;和&nbsp;s2&nbsp;。一次&nbsp;字符串交换&nbsp;操作的步骤如下:选出某个字符串中的两个下标(不必不同),并交换这两个下标所对应的字符。如果对&nbsp;其中一个字符串&nbsp;执行&nbsp;最多一次字符串交换&nbsp;就可以使两个字符串相等,返回&nbsp;true&nbsp;;否则,返回&nbsp;false&nbsp;。示例&nbsp;1:输入:s1&nbsp;=&nbsp;&amp;quot;bank&amp;quot;,&nbsp;s2&nbsp;=&nbsp;&amp;quot;kanb&amp;quot;输出:true解释:例如,交换&nbsp;s2&nbsp;中的第一个和最后一个字符可以得到&nbsp;&amp;quot;bank&amp;quot;示例&nbsp;2:输入:s1&nbsp;=&nbsp;&amp;quot;attack&amp;quot;,&nbsp;s2&nbsp;=&nbsp;&amp;quot;defend&amp;quot;输出:false解释:一次字符串交换无法使两个字符串相等示例&nbsp;3:输入:s1&nbsp;=&nbsp;&amp;quot;kelb&amp;quot;,&nbsp;s2&nbsp;=&nbsp;&amp;quot;kelb&amp;quot;输出:true解释:两个字符串已经相等,所以不需要进行字符串交换示例&nbsp;4:输入:s1&nbsp;=&nbsp;&amp;quot;abcd&amp;quot;,&nbsp;s2&nbsp;=&nbsp;&amp;quot;dcba&amp;quot;输出:false提示:1&nbsp;&lt;=&nbsp;s1.length,&nbsp;s2.length&nbsp;&lt;=&nbsp;100s1.length&nbsp;==&nbsp;s2.lengths1&nbsp;和&nbsp;s2&nbsp;仅由小写英文字母组成答案评论区留下你的看法,欢迎讨论华为OD机试2024年D卷真题目录&nbsp;&nbsp;华为OD机试(D卷)2024真题目录(全、新、准)_牛客网 https://www.nowcoder.com/discuss/637324711520681984
查看1道真题和解析
点赞 评论 收藏
分享
2 1 评论
分享
牛客网
牛客企业服务