Skip to main content

Check if a String Is an Acronym of Words - Solution & Explanation

EasyArrayString17 min readAsked at: Amazon, Uber
Practice this problem

Problem Statement

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.

Approach Overview

Problem Overview: You receive an array of words and a string s. The task is to determine whether s equals the acronym formed by taking the first character of every word in the array, in order.

Approach 1: Iterative String Concatenation (O(n) time, O(n) space)

Build the acronym explicitly. Iterate through the array of words and append the first character of each word to a temporary string. After the loop, compare the generated acronym with s. If both strings are identical, return true; otherwise return false. This approach is straightforward and easy to reason about because you recreate the expected acronym before comparing it.

The main cost comes from constructing the new string, which requires O(n) time for n words and O(n) extra space to store the acronym. This approach is useful when clarity is more important than strict memory efficiency.

Approach 2: Index-Based Character Matching (O(n) time, O(1) space)

A more space-efficient approach compares characters directly without building a new string. First check if s.length() equals the number of words. If the lengths differ, the acronym cannot match. Then iterate through the word list and compare the first character of each word with the corresponding character in s.

If any comparison fails, return false immediately. If the loop completes successfully, the acronym matches and you return true. This method relies on simple index access in a string and sequential traversal of the array.

The algorithm runs in O(n) time because each word is inspected once. Space complexity is O(1) since no additional data structures are created. The early length check also prevents unnecessary comparisons.

Recommended for interviews: Index-Based Character Matching is typically expected. It demonstrates awareness of unnecessary allocations and keeps the solution clean and efficient. Showing the concatenation approach first proves you understand the problem transformation, but the index-based method shows stronger attention to space optimization and early validation checks.

Approach 1: Iterative String Concatenation

This approach involves iterating over each word, extracting the first character, and concatenating these characters to form a new string. Finally, compare this concatenated string with the string s. If they are equal, s is an acronym; otherwise, it is not.

This C code iterates through each word in the array to collect the first character, concatenates them into a string, and compares this new string with s using strcmp.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of `words`.
Space Complexity: O(1), aside from input storage.

Try this approach in the editor →

Approach 2: Index-Based Character Matching

For this approach, directly compare the i-th character in s with the first character of the i-th word in words. This eliminates the need for an additional string construction, thus potentially speeding up the execution by doing the comparison simultaneously during the iteration.

This C solution checks if the length of `s` matches the number of words. If not, it directly returns false. Otherwise, it iterates over each word and checks if the corresponding character in `s` matches the first character of the word.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of `s` (or words since they must be the same).
Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Simulation

We can iterate over each string in the array words, concatenate their first letters to form a new string t, and then check if t is equal to s.

The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the array words.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Approach 4: Simulation (Space Optimization)

First, we check if the number of strings in words is equal to the length of s. If not, s is definitely not an acronym of the first letters of words, and we directly return false.

Then, we iterate over each character in s, checking if it is equal to the first letter of the corresponding string in words. If not, s is definitely not an acronym of the first letters of words, and we directly return false.

After the iteration, if we haven't returned false, then s is an acronym of the first letters of words, and we return true.

The time complexity is O(n), where n is the length of the array words. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterative String Concatenation

Time Complexity: O(n), where n is the length of `words`.
Space Complexity: O(1), aside from input storage.

Index-Based Character Matching

Time Complexity: O(n), where n is the length of `s` (or words since they must be the same).
Space Complexity: O(1).

Simulation—
Simulation (Space Optimization)—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative String ConcatenationO(n)O(n)When readability and explicit acronym construction help understanding
Index-Based Character MatchingO(n)O(1)General case and interview settings where constant space is preferred

Video Solution

Leetcode | 2828. Check if a String is an Acronym of Words | Easy | Java Solution • Developer Docs • 813 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Check if a String Is an Acronym of Words easy or hard?
Check if a String Is an Acronym of Words is classified as an Easy problem. It focuses on straightforward string and array operations, making it a common introductory exercise for practicing indexing and iteration.
Check if a String Is an Acronym of Words Python/Java solution
In Python or Java, iterate through the list of words and compare each word's first character with the corresponding character in the string. Return false immediately on mismatch. This implementation runs in O(n) time and uses constant extra space.
How to solve Check if a String Is an Acronym of Words in O(n)?
Start by verifying that the length of the acronym string equals the number of words. Then iterate through the array and compare the first character of each word with the character at the same index in the string. If all comparisons match, the string is a valid acronym.
What is the best approach for Check if a String Is an Acronym of Words?
The most efficient approach is index-based character matching. Compare the first character of each word with the corresponding character in the target string while iterating through the array. This avoids building a new string and runs in O(n) time with O(1) extra space.
Is Check if a String Is an Acronym of Words asked at Google/Amazon/Meta?
Problems like this appear in coding interviews at large tech companies as warm-up or screening questions. They test basic string manipulation, array traversal, and attention to edge cases rather than complex algorithms.
What data structure is used in Check if a String Is an Acronym of Words?
The solution primarily uses an array (or list) of strings and a single string for comparison. No advanced data structures are required, making it a basic exercise in array traversal and string indexing.
What is the time complexity of Check if a String Is an Acronym of Words?
The time complexity is O(n), where n is the number of words in the array. Each word is visited once to check its first character against the corresponding position in the acronym string.

Ready to solve this problem?

Practice Check if a String Is an Acronym of Words with our built-in code editor and test cases.

Practice on FleetCode