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 <= 1001 <= words[i].length <= 101 <= s.length <= 100words[i] and s consist of lowercase English letters
Hints for Check if a String Is an Acronym of Words
Hint 1
The acronym has exactly one character per word, so a length mismatch already decides the answer.
Hint 2
Otherwise compare
s[i]with the first character ofwords[i]for everyi.
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
- If
s.length != words.length, returnfalse. - For each index
i, ifwords[i][0] != s[i], returnfalse. - 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
sthat matches a prefix of the acronym is stillfalse.
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 TrueJava
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.