From 6595d65048fa0d86594dda17c84a1c971e14b11f Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Tue, 31 Mar 2026 19:40:40 +0800 Subject: [PATCH] feat: add leetcode 2828 - check if a string is an acronym of words Add implementation for LeetCode problem 2828 with: - O(n) time complexity solution using StringBuilder - Comprehensive Javadoc with complexity analysis - Documentation updates (README.md and docs/EASY.md) --- README.md | 4 +- docs/EASY.md | 3 +- src/CheckIfAStringIsAnAcronymOfWords2828.java | 22 ---------- .../CheckIfAStringIsAnAcronymOfWords2828.java | 40 +++++++++++++++++++ 4 files changed, 44 insertions(+), 25 deletions(-) delete mode 100644 src/CheckIfAStringIsAnAcronymOfWords2828.java create mode 100644 src/main/java/com/leetcode/easy/CheckIfAStringIsAnAcronymOfWords2828.java diff --git a/README.md b/README.md index 6e40833..b5bffd7 100644 --- a/README.md +++ b/README.md @@ -44,11 +44,11 @@ Each problem follows this structure: ## Solutions -📊 **Statistics**: 51 problems solved (migrated to new structure) +📊 **Statistics**: 52 problems solved (migrated to new structure) | Difficulty | Count | |------------|-------| -| Easy | 35 | +| Easy | 36 | | Medium | 15 | | Hard | 1 | diff --git a/docs/EASY.md b/docs/EASY.md index a3ac856..37bda59 100644 --- a/docs/EASY.md +++ b/docs/EASY.md @@ -1,6 +1,6 @@ # Easy Problems -Total: 35 problems solved +Total: 36 problems solved ## Solutions @@ -40,6 +40,7 @@ Total: 35 problems solved | 2315 | [Count Asterisks](https://leetcode.com/problems/count-asterisks/) | [CountAsterisks2315.java](../src/main/java/com/leetcode/easy/CountAsterisks2315.java) | [CountAsterisks2315Test.java](../src/test/java/com/leetcode/easy/CountAsterisks2315Test.java) | | 2469 | [Convert the Temperature](https://leetcode.com/problems/convert-the-temperature/) | [ConvertTheTemperature2469.java](../src/main/java/com/leetcode/easy/ConvertTheTemperature2469.java) | [ConvertTheTemperature2469Test.java](../src/test/java/com/leetcode/easy/ConvertTheTemperature2469Test.java) | | 2798 | [Number of Employees Who Met the Target](https://leetcode.com/problems/number-of-employees-who-met-the-target/) | [NumberOfEmployeesWhoMetTheTarget2798.java](../src/main/java/com/leetcode/easy/NumberOfEmployeesWhoMetTheTarget2798.java) | [NumberOfEmployeesWhoMetTheTarget2798Test.java](../src/test/java/com/leetcode/easy/NumberOfEmployeesWhoMetTheTarget2798Test.java) | +| 2828 | [Check If A String Is An Acronym Of Words](https://leetcode.com/problems/check-if-a-string-is-an-acronym-of-words/) | [CheckIfAStringIsAnAcronymOfWords2828.java](../src/main/java/com/leetcode/easy/CheckIfAStringIsAnAcronymOfWords2828.java) | | | 3289 | [The Two Sneaky Numbers of Digitville](https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville/) | [TheTwoSneakyNumbersOfDigitville3289.java](../src/main/java/com/leetcode/easy/TheTwoSneakyNumbersOfDigitville3289.java) | [TheTwoSneakyNumbersOfDigitville3289Test.java](../src/test/java/com/leetcode/easy/TheTwoSneakyNumbersOfDigitville3289Test.java) | --- diff --git a/src/CheckIfAStringIsAnAcronymOfWords2828.java b/src/CheckIfAStringIsAnAcronymOfWords2828.java deleted file mode 100644 index 62c8ac1..0000000 --- a/src/CheckIfAStringIsAnAcronymOfWords2828.java +++ /dev/null @@ -1,22 +0,0 @@ -import java.util.ArrayList; -import java.util.List; - -public class CheckIfAStringIsAnAcronymOfWords2828 { - public boolean isAcronym(List words, String s) { - String acronym = ""; - for (String word : words) { - char[] chars = word.toCharArray(); - acronym += chars[0]; - } - return acronym.equals(s); - } - - public static void main(String[] args) { - List words = new ArrayList<>(); - words.add("alice"); - words.add("bob"); - words.add("charlie"); - String s = "abc"; - assert new CheckIfAStringIsAnAcronymOfWords2828().isAcronym(words, s); - } -} diff --git a/src/main/java/com/leetcode/easy/CheckIfAStringIsAnAcronymOfWords2828.java b/src/main/java/com/leetcode/easy/CheckIfAStringIsAnAcronymOfWords2828.java new file mode 100644 index 0000000..343ae61 --- /dev/null +++ b/src/main/java/com/leetcode/easy/CheckIfAStringIsAnAcronymOfWords2828.java @@ -0,0 +1,40 @@ +// Tags: Array, String +package com.leetcode.easy; + +import java.util.List; + +public class CheckIfAStringIsAnAcronymOfWords2828 { + /** + * Checks if a string is an acronym of the given list of words. + *

+ * An acronym is formed by taking the first character of each word + * and concatenating them in order. + *

+ * Time Complexity: O(n) + *

    + *
  • n = number of words in the list
  • + *
  • Size comparison check: O(1)
  • + *
  • Single pass through words list: O(n)
  • + *
  • String comparison: O(n)
  • + *
+ *

+ * Space Complexity: O(n) + *

    + *
  • StringBuilder stores n characters: O(n)
  • + *
+ * + * @param words list of non-empty strings + * @param s the string to check if it's the acronym + * @return {@code true} if s is the acronym of words, {@code false} otherwise + */ + public boolean isAcronym(List words, String s) { + if (words.size() != s.length()) { + return false; + } + StringBuilder sb = new StringBuilder(); + for (String word : words) { + sb.append(word.charAt(0)); + } + return sb.toString().equals(s); + } +}