Leetcode 2828: Check if a String Is an Acronym of Words

grid47
grid47
Exploring patterns and algorithms
Jan 29, 2024 4 min read

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.
Problem
Approach
Steps
Complexity
Input: The input consists of an array `words` of strings and a string `s`.
Example: For example, `words = ['orange', 'pear', 'grape']` and `s = 'opg'`
Constraints:
• 1 <= words.length <= 100
• 1 <= words[i].length <= 10
• 1 <= s.length <= 100
• words[i] and s consist of lowercase English letters.
Output: Return `true` if `s` is an acronym of `words`, otherwise return `false`.
Example: For `words = ['dog', 'cat', 'fish']` and `s = 'dcf'`, the output is `true`.
Constraints:
Goal: The goal is to check if the string `s` matches the concatenation of the first characters from each string in `words`.
Steps:
• Initialize an empty string to store the acronym.
• For each word in `words`, add its first character to the acronym string.
• Compare the acronym with `s` and return `true` if they match, otherwise return `false`.
Goal: The input will consist of a list of strings and a target acronym string.
Steps:
• The length of `words` is between 1 and 100.
• Each word in `words` has a length between 1 and 10.
• The length of `s` is between 1 and 100.
Assumptions:
• The input strings only contain lowercase English letters.
• The solution should work within the constraints for both the `words` array and string `s`.
Input: For `words = ['orange', 'pear', 'grape']` and `s = 'opg'`
Explanation: The first characters of the words 'orange', 'pear', and 'grape' are 'o', 'p', and 'g', respectively. Hence, `s = 'opg'` is the acronym.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus