Check if a String Is an Acronym of Words

EASY

Description

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.

Approaches

Checkout 2 different approaches to solve Check if a String Is an Acronym of Words. Click on different approaches to view the approach and algorithm in detail.

Build Acronym and Compare

This approach involves first constructing the potential acronym from the words list and then comparing it with the given string s.

Algorithm

  • Initialize an empty StringBuilder.
  • Iterate through each word in the words list.
  • Append the first character of the word (word.charAt(0)) to the StringBuilder.
  • After the loop, convert the StringBuilder to a string.
  • Return the result of comparing this new string with s.

This method directly translates the problem definition into code. It uses a StringBuilder for efficient string concatenation. We iterate through the list of words, appending the first character of each word to the StringBuilder. Once the full acronym string is built, it is converted to a String and compared with the input string s. While this is very clear and easy to implement, it requires additional memory to hold the newly constructed string.

import java.util.List;

class Solution {
    public boolean isAcronym(List<String> words, String s) {
        StringBuilder acronymBuilder = new StringBuilder();
        for (String word : words) {
            acronymBuilder.append(word.charAt(0));
        }
        return acronymBuilder.toString().equals(s);
    }
}

Complexity Analysis

Time Complexity: `O(N)`, where `N` is the number of strings in `words`. The loop runs `N` times, and the final string comparison also takes `O(N)` time.Space Complexity: `O(N)`, as a `StringBuilder` (and then a `String`) of length `N` is created to store the acronym, where `N` is the number of words.

Pros and Cons

Pros:
  • Very straightforward and easy to understand.
  • Code directly reflects the problem's definition.
Cons:
  • Inefficient in terms of space usage.
  • Builds the entire string even if a mismatch could be found early, such as differing lengths.

Code Solutions

Checking out 3 solutions in different languages for Check if a String Is an Acronym of Words. Click on different languages to view the code.

class Solution {
public
  boolean isAcronym(List<String> words, String s) {
    StringBuilder t = new StringBuilder();
    for (var w : words) {
      t.append(w.charAt(0));
    }
    return t.toString().equals(s);
  }
}

Video Solution

Watch the video walkthrough for Check if a String Is an Acronym of Words



Data Structures:

ArrayString

Subscribe to Scale Engineer newsletter

Learn about System Design, Software Engineering, and interview experiences every week.

No spam, unsubscribe at any time.