LeetCode 2828: Check if a String Is an Acronym of Words Solution
Master LeetCode problem 2828 (Check if a String Is an Acronym of Words), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
Problem Explanation
Explanation
To solve this problem, we iterate through each word in the words array and compare the first character of each word with the corresponding character in the string s. If all characters match, we return true; otherwise, we return false. We need to ensure that the lengths of words and s are the same.
- Time Complexity: O(n), where n is the number of words in the
wordsarray. - Space Complexity: O(1)
Solution Code
class Solution {
public 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;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2828 (Check if a String Is an Acronym of Words)?
This page provides optimized solutions for LeetCode problem 2828 (Check if a String Is an Acronym of Words) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2828 (Check if a String Is an Acronym of Words)?
The time complexity for LeetCode 2828 (Check if a String Is an Acronym of Words) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2828 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2828 in Java, C++, or Python.