Check if a String Is an Acronym of Words

Easy
1759.

Check if a String Is an Acronym of Words

Check if a String Is an Acronym of Words is an easy coding problem on arrays and strings, asked in Amazon and Google interviews.

You are given an array of lowercase strings words and a string s.

The acronym of words is the string formed by taking the first character of every word, in order. For example, the acronym of ["code","kairo","rocks"] is "ckr".

Return true if s is exactly the acronym of words, and false otherwise.

Example 1

Input
words = ["alice","bob","charlie"], s = "abc"
Output
true
Explanation
The first letters are a, b and c.

Example 2

Input
words = ["an","apple"], s = "a"
Output
false
Explanation
The acronym is "aa"; "a" is only part of it.

Example 3

Input
words = ["never","gonna","give","up","on","you"], s = "ngguoy"
Output
true

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 10
  • 1 <= s.length <= 100
  • words[i] and s consist of lowercase English letters
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string[]} words
 * @param {string} s
 * @return {boolean}
 */
var isAcronym = function(words, s) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Check if a String Is an Acronym of Words — Solution

Check if a String Is an Acronym of Words solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Check if a String Is an Acronym of Words is an easy coding problem on arrays and strings, asked in Amazon and Google interviews.

  • Difficulty: Easy
  • Topics: Arrays, Strings
  • Asked at: Amazon, Google
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

You are given an array of lowercase strings words and a string s.

The acronym of words is the string formed by taking the first character of every word, in order. For example, the acronym of ["code","kairo","rocks"] is "ckr".

Return true if s is exactly the acronym of words, and false otherwise.

Example 1

Input: words = ["alice","bob","charlie"], s = "abc"
Output: true
Explanation: The first letters are a, b and c.

Example 2

Input: words = ["an","apple"], s = "a"
Output: false
Explanation: The acronym is "aa"; "a" is only part of it.

Example 3

Input: words = ["never","gonna","give","up","on","you"], s = "ngguoy"
Output: true

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 10
  • 1 <= s.length <= 100
  • words[i] and s consist of lowercase English letters

Hints for Check if a String Is an Acronym of Words

  1. Hint 1

    The acronym has exactly one character per word, so a length mismatch already decides the answer.

  2. Hint 2

    Otherwise compare s[i] with the first character of words[i] for every i.

How to solve Check if a String Is an Acronym of Words

The acronym is fully determined by words: one character per word, in order. So s matches it only if it has the same length and agrees with every word's first letter.

Approach

  1. If s.length != words.length, return false.
  2. For each index i, if words[i][0] != s[i], return false.
  3. Return true.

Why it works

Two strings are equal exactly when their lengths agree and they agree position by position; the acronym's i-th character is words[i][0] by definition.

Complexity

  • Time — O(n)
  • Space — O(1)

Pitfalls

  • Check the length first — a shorter s that matches a prefix of the acronym is still false.

Reference solution

Python

from typing import List

def isAcronym(words: List[str], s: str) -> bool:
    if len(words) != len(s):
        return False
    for w, c in zip(words, s):
        if w[0] != c:
            return False
    return True

Java

public static boolean isAcronym(String[] words, String s) {
    if (words.length != s.length()) return false;
    for (int i = 0; i < words.length; i++) {
        if (words[i].charAt(0) != s.charAt(i)) return false;
    }
    return true;
}

C++

bool isAcronym(vector<string>& words, string s) {
    if (words.size() != s.size()) return false;
    for (size_t i = 0; i < words.size(); i++) {
        if (words[i][0] != s[i]) return false;
    }
    return true;
}

JavaScript

var isAcronym = function(words, s) {
    if (words.length !== s.length) return false;
    for (var i = 0; i < words.length; i++) {
        if (words[i].charAt(0) !== s.charAt(i)) return false;
    }
    return true;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Strings