Check if a String Is an Acronym of Words - Problem

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.

Input & Output

Example 1 — Basic Valid Acronym
$ Input: words = ["alice","bob","charlie"], s = "abc"
Output: true
💡 Note: Taking first letters: 'a' from "alice", 'b' from "bob", 'c' from "charlie" gives "abc" which matches s
Example 2 — Invalid Acronym
$ Input: words = ["an","apple"], s = "a"
Output: false
💡 Note: First letters are 'a' and 'a', forming "aa", but s = "a" has different length
Example 3 — Single Character Match
$ Input: words = ["never"], s = "n"
Output: true
💡 Note: Single word "never" has first letter 'n' which matches s = "n"

Constraints

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

Visualization

Tap to expand
Check if 'abc' is acronym of ['alice', 'bob', 'charlie']Input Words:alicebobcharlieTarget String:abcExtract First Letters:abcFormed: 'abc' == Target: 'abc' ✓Result: true
Understanding the Visualization
1
Input
Array of words and target string
2
Extract
Take first character of each word
3
Compare
Check if extracted characters match target string
Key Takeaway
🎯 Key Insight: Simply compare first character of each word with corresponding position in target string
Asked in
Google 25 Amazon 20 Microsoft 15
28.1K Views
Medium Frequency
~8 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen