Ben Pereira

Posted on

# Leet Code — 2828. Check if a String Is an Acronym of Words

It’s an easy problem from leet code that says:

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.

One way to get this problem solved would be to go on each first character of that List and retrieve it, join then together and then do a equals comparison, one liner solution would be:

``````class Solution {
public boolean isAcronym(List<String> words, String s) {
return words.stream().map(w -> String.valueOf(w.charAt(0))).collect(Collectors.joining("")).equals(s);
}
}
``````

Runtime: 9 ms, faster than 9.91% of Java online submissions for Check if a String Is an Acronym of Words.

Memory Usage: 44.1 MB, less than 8.29% of Java online submissions for Check if a String Is an Acronym of Words.

This map is not that great performance wise here since it creates strings along the way, you could use Collector.of method and append the string builders but seems more messy than usual.

I would suggest you to go on a simple route that is basically not using stream and making a simple iteration:

``````class Solution {
public boolean isAcronym(List<String> words, String s) {
final StringBuilder builder = new StringBuilder();
for(int i=0;i<words.size();i++){
builder.append(words.get(i).charAt(0));
}
return builder.toString().equals(s);
}
}
``````

Runtime: 1 ms, faster than 100.00% of Java online submissions for Check if a String Is an Acronym of Words.

Memory Usage: 43.3 MB, less than 93.98% of Java online submissions for Check if a String Is an Acronym of Words.

Another way to get it done would be iteration of words and then comparing first character of each, also making sure the index of s is not out of bounds and at the end checking again since it could go later as well (not just when iterating).

``````class Solution {
public boolean isAcronym(List<String> words, String s) {
int sIndex=0;
for(int i=0 ; i < words.size() ; i++) {
if(sIndex == s.length()) return false;
if(words.get(i).charAt(0) != s.charAt(sIndex)) return false;
sIndex++;
}
return sIndex == s.length();
}
}
``````

Runtime: 1 ms, faster than 100.00% of Java online submissions for Check if a String Is an Acronym of Words.

Memory Usage: 43.9 MB, less than 36.32% of Java online submissions for Check if a String Is an Acronym of Words.

That’s it! If there is anything thing else to discuss feel free to drop a comment, until next post! :)