Check if a String Is an Acronym of Words - Problem

You're tasked with creating an acronym validator! Given an array of strings words and a string s, determine if s forms a valid acronym of the words.

An acronym is formed by taking the first character of each word in the given order. For example:

  • "NASA" could be formed from ["National", "Aeronautics", "Space", "Administration"]
  • "FBI" could be formed from ["Federal", "Bureau", "Investigation"]

Important: The order matters! "ab" can be formed from ["apple", "banana"], but cannot be formed from ["banana", "apple"].

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

Input & Output

example_1.py โ€” Basic Valid Acronym
$ Input: words = ["alice", "bob", "charlie"], s = "abc"
โ€บ Output: true
๐Ÿ’ก Note: Taking the first character of each word: 'a' from "alice", 'b' from "bob", 'c' from "charlie" gives us "abc", which matches the input string s.
example_2.py โ€” Invalid Acronym
$ Input: words = ["an", "apple"], s = "a"
โ€บ Output: false
๐Ÿ’ก Note: The first characters would be 'a' from "an" and 'a' from "apple", giving us "aa". Since "aa" โ‰  "a", this is not a valid acronym.
example_3.py โ€” Length Mismatch
$ Input: words = ["never", "gonna", "give", "up"], s = "nggu"
โ€บ Output: true
๐Ÿ’ก Note: Taking first characters: 'n' from "never", 'g' from "gonna", 'g' from "give", 'u' from "up" gives us "nggu", which exactly matches the input string.

Visualization

Tap to expand
Acronym Formation VisualizationInput Words:"Federal""Bureau""Investigation"Target: "FBI"FBIExtract first char:FBIโœ“ F == Fโœ“ B == Bโœ“ I == IResult: VALID ACRONYMAll characters match in correct orderTime: O(n) | Space: O(1)
Understanding the Visualization
1
Input Validation
Check if the number of words matches the length of target acronym
2
Character Extraction
Take the first character from each word in sequence
3
Direct Comparison
Compare extracted characters with target string positions
4
Result
Return true if all characters match, false otherwise
Key Takeaway
๐ŸŽฏ Key Insight: The most efficient approach compares characters directly without building intermediate strings, achieving O(1) space complexity while maintaining O(n) time complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

We iterate through the words array once, checking each character. In worst case, we check all n characters

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

We only use a constant amount of extra space for variables, no additional data structures

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค words.length โ‰ค 100
  • 1 โ‰ค words[i].length โ‰ค 10
  • 1 โ‰ค s.length โ‰ค 100
  • words[i] and s consist of lowercase English letters
  • Order matters - acronym must be formed in the exact sequence given
Asked in
Google 25 Amazon 18 Microsoft 12 Apple 8
28.5K Views
Medium Frequency
~8 min Avg. Time
890 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