algoadvance

Leetcode 2828. Check if a String Is an Acronym of Words

Problem Statement

You are given a list of strings words and a string s. The task is to determine if s is an acronym of the strings in words. A string s is considered an acronym of words if it is equal to the concatenation of the first letter of each word in words in order.

Example:

Output: True

Clarifying Questions

  1. Case Sensitivity: Should we consider the case while comparing the acronym?
    • Assume that the comparison is case-sensitive as the problem statement does not specify otherwise.
  2. Input Constraints: Are words and s always non-empty and contain only alphabetical characters?
    • Assume words and s are non-empty, and each string in words contains at least one character.
  3. Length of Strings: What is the maximum length for words and s?
    • Assume reasonable constraints such as 1 <= words.length <= 1000 and the length of each word and s being <= 1000 characters.

Strategy

  1. Initialize an empty string to store the generated acronym.
  2. Iterate through each word in words and concatenate the first letter of each word to the acronym string.
  3. Compare the generated acronym string with s.
  4. Return True if they are equal, otherwise return False.

Time Complexity

Code

#include <vector>
#include <string>

bool isAcronym(std::vector<std::string>& words, std::string s) {
    std::string acronym = "";
    for (const std::string& word : words) {
        acronym += word[0];
    }
    return acronym == s;
}

This code correctly follows the strategy and checks if s is an acronym of the words list.

Cut your prep time in half and DOMINATE your interview with AlgoAdvance AI