leetcode
leetcode 2401 ~ 2450
判别首字母缩略词

判别首字母缩略词

难度:

标签:

题目描述

Given an array of strings words and a string s, determine if s is an acronym of words.

The string s is considered an acronym of words if it can be formed by concatenating the first character of each string in words in order. For example, "ab" can be formed from ["apple", "banana"], but it can't be formed from ["bear", "aardvark"].

Return true if s is an acronym of words, and false otherwise.

 

Example 1:

Input: words = ["alice","bob","charlie"], s = "abc"
Output: true
Explanation: The first character in the words "alice", "bob", and "charlie" are 'a', 'b', and 'c', respectively. Hence, s = "abc" is the acronym. 

Example 2:

Input: words = ["an","apple"], s = "a"
Output: false
Explanation: The first character in the words "an" and "apple" are 'a' and 'a', respectively. 
The acronym formed by concatenating these characters is "aa". 
Hence, s = "a" is not the acronym.

Example 3:

Input: words = ["never","gonna","give","up","on","you"], s = "ngguoy"
Output: true
Explanation: By concatenating the first character of the words in the array, we get the string "ngguoy". 
Hence, s = "ngguoy" is the acronym.

 

Constraints:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 10
  • 1 <= s.length <= 100
  • words[i] and s consist of lowercase English letters.

代码结果

运行时间: 16 ms, 内存: 16.5 MB


/**
 * Solution using Java Streams to check if a string s is an acronym of an array of words.
 * The approach uses streams to map the first letter of each word and collect them into a single string.
 */
import java.util.Arrays;
import java.util.stream.Collectors;

public class AcronymCheckerStream {
    public boolean isAcronym(String[] words, String s) {
        // Check if the length of s is the same as the number of words
        if (words.length != s.length()) return false;
        
        // Use streams to map the first character of each word to a string
        String acronym = Arrays.stream(words)
                               .map(word -> String.valueOf(word.charAt(0)))
                               .collect(Collectors.joining());
        
        // Compare the built acronym with s
        return acronym.equals(s);
    }
}

解释

方法:

该题解首先检查输入字符串数组 `words` 的长度是否与字符串 `s` 的长度相等,因为只有当两者长度相同时,才有可能通过 `words` 的每个单词的第一个字符完全匹配字符串 `s`。接下来,题解通过遍历 `words`,对于每个单词,检查它的第一个字符是否与 `s` 中对应位置的字符相同。如果所有字符都匹配,则返回 `true`;否则,在发现第一个不匹配的字符时立即返回 `false`。

时间复杂度:

O(n)

空间复杂度:

O(1)

代码细节讲解

🦆
在题解中,检查`words`数组长度与`s`长度是否相等的逻辑是基于什么理由?
检查`words`数组长度与`s`长度是否相等的逻辑基于题目的需求,即判断字符串`s`是否可以被`words`中每个单词的首字母缩略而形成。只有当`words`的长度(即单词的数量)与`s`的长度完全相同时,每个单词的首字母才有可能按顺序完全匹配字符串`s`中的每一个字符。如果长度不相等,显然无法通过首字母形成完全相同的字符串。
🦆
如果`words`中的某个字符串为空,题解中的`e[0]`表达式将如何处理?是否有可能引发错误?
如果`words`中包含空字符串,那么在尝试访问`e[0]`时将会引发错误,因为空字符串没有第一个字符。在Python中,这将引发`IndexError`异常,因为索引0在空字符串中不存在。因此,题解应当在实际应用中加入对空字符串的检查,以避免程序崩溃。
🦆
题解在遍历`words`时,为何选择了`enumerate`函数?使用`enumerate`与直接通过索引访问有什么不同或优势?
题解中使用`enumerate`函数是因为它同时提供元素的索引和值,这在许多情况下非常有用。在本题中,使用`enumerate`可以同时获取单词及其在列表中的位置,这样可以直接与字符串`s`中对应位置的字符进行比较。如果不使用`enumerate`,则需要另外维护一个索引计数器或者使用`range(len(words))`进行循环,这相比于`enumerate`使用起来更繁琐,代码也不够简洁。
🦆
当所有条件满足,为何题解中使用了`else`语句块来返回`True`?这里的`else`与常规使用有何不同?
在题解中使用的`else`语句块是与`for`循环相关联的,这是Python特有的一种用法。这个`else`语句在`for`循环正常结束后执行,即没有被`break`语句中断。在这个场景中,如果所有的字符都成功匹配,循环会正常结束,然后执行`else`块中的`return True`。如果在循环中发现不匹配的字符并执行了`return False`,则`else`块不会被执行。这样使用`else`可以更清晰地表达'所有检查都通过后返回True'的逻辑。

相关问题