Magicsheet logo

Check if a String Is an Acronym of Words

Easy
12.5%
Updated 8/1/2025

Asked by 2 Companies

Check if a String Is an Acronym of Words

What is this problem about?

The "Check if a String Is an Acronym of Words interview question" is a straightforward string verification task. You are given an array of words and a target string s. You need to determine if s is the acronym formed by taking the first character of each word in the array in order. For example, the acronym for ["apple", "banana"] is "ab".

Why is this asked in interviews?

Amazon and Uber use the "Check if a String Is an Acronym coding problem" as an early-round screening question. It tests a candidate's ability to handle basic arrays and strings, perform linear comparisons, and check for length mismatches. It’s a test of precision and clean code implementation.

Algorithmic pattern used

This problem follows the Linear Scan pattern.

  1. Length Check: If the number of words in the array does not match the length of the string s, it cannot be a valid acronym. Return false immediately.
  2. Comparison: Iterate through the array of words. For each word at index i, check if word[0] is equal to s[i].
  3. Termination: If any character doesn't match, return false. Otherwise, return true.

Example explanation

Words: ["hello", "world"], s="hw"s = "hw"

  1. Length of words (2) matches length of ss (2).
  2. index 0: words[0] is "hello", first char is 'h'. s[0] is 'h'. Match!
  3. index 1: words[1] is "world", first char is 'w'. s[1] is 'w'. Match! Result: True.

Common mistakes candidates make

  • Ignoring Length: Forgetting to check if the length of the array matches the length of the string, which can lead to index out of bounds or false positives.
  • Case Sensitivity: Not clarifying if the comparison should be case-sensitive (though usually it is for these problems).
  • Concatenation: Creating a new acronym string first and then comparing it to ss. While correct, it uses O(N)O(N) extra space. Comparing character-by-character is O(1)O(1) extra space.

Interview preparation tip

Always look for "early exit" conditions in simple validation problems. If the lengths don't match, you don't even need to look at the words. This shows you are thinking about edge cases and optimization.

Similar Questions