diff --git a/README.md b/README.md index 2acd909..24ebcf2 100644 --- a/README.md +++ b/README.md @@ -44,11 +44,11 @@ Each problem follows this structure: ## Solutions -📊 **Statistics**: 54 problems solved (migrated to new structure) +📊 **Statistics**: 55 problems solved (migrated to new structure) | Difficulty | Count | |------------|-------| -| Easy | 38 | +| Easy | 39 | | Medium | 15 | | Hard | 1 | diff --git a/docs/EASY.md b/docs/EASY.md index 683f774..456d8a0 100644 --- a/docs/EASY.md +++ b/docs/EASY.md @@ -1,6 +1,6 @@ # Easy Problems -Total: 38 problems solved +Total: 39 problems solved ## Solutions @@ -32,6 +32,7 @@ Total: 38 problems solved | 1603 | [Design Parking System](https://leetcode.com/problems/design-parking-system/) | [DesignParkingSystem1603.java](../src/main/java/com/leetcode/easy/DesignParkingSystem1603.java) | [DesignParkingSystem1603Test.java](../src/test/java/com/leetcode/easy/DesignParkingSystem1603Test.java) | | 1662 | [Check If Two String Arrays are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/) | [CheckIfTwoStringArraysAreEquivalent1662.java](../src/main/java/com/leetcode/easy/CheckIfTwoStringArraysAreEquivalent1662.java) | [CheckIfTwoStringArraysAreEquivalent1662Test.java](../src/test/java/com/leetcode/easy/CheckIfTwoStringArraysAreEquivalent1662Test.java) | | 1672 | [Richest Customer Wealth](https://leetcode.com/problems/richest-customer-wealth/) | [RichestCustomerWealth1672.java](../src/main/java/com/leetcode/easy/RichestCustomerWealth1672.java) | [RichestCustomerWealth1672Test.java](../src/test/java/com/leetcode/easy/RichestCustomerWealth1672Test.java) | +| 1684 | [Count the Number of Consistent Strings](https://leetcode.com/problems/count-the-number-of-consistent-strings/) | [CountTheNumberOfConsistentStrings1684.java](../src/main/java/com/leetcode/easy/CountTheNumberOfConsistentStrings1684.java) | [CountTheNumberOfConsistentStrings1684Test.java](../src/test/java/com/leetcode/easy/CountTheNumberOfConsistentStrings1684Test.java) | | 1773 | [Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule/) | [CountItemsMatchingARule1773.java](../src/main/java/com/leetcode/easy/CountItemsMatchingARule1773.java) | [CountItemsMatchingARule1773Test.java](../src/test/java/com/leetcode/easy/CountItemsMatchingARule1773Test.java) | | 1816 | [Truncate Sentence](https://leetcode.com/problems/truncate-sentence/) | [TruncateSentence1816.java](../src/main/java/com/leetcode/easy/TruncateSentence1816.java) | [TruncateSentence1816Test.java](../src/test/java/com/leetcode/easy/TruncateSentence1816Test.java) | | 1941 | [Check if All Characters Have Equal Number of Occurrences](https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/) | [CheckIfAllCharactersHaveEqualNumberOfOccurrences1941.java](../src/main/java/com/leetcode/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941.java) | [CheckIfAllCharactersHaveEqualNumberOfOccurrences1941Test.java](../src/test/java/com/leetcode/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941Test.java) | diff --git a/src/CountTheNumberOfConsistentStrings1684.java b/src/CountTheNumberOfConsistentStrings1684.java deleted file mode 100644 index d6d6a10..0000000 --- a/src/CountTheNumberOfConsistentStrings1684.java +++ /dev/null @@ -1,35 +0,0 @@ -import java.util.HashSet; -import java.util.Set; - -public class CountTheNumberOfConsistentStrings1684 { - public int countConsistentStrings(String allowed, String[] words) { - Set setChars = new HashSet<>(); - char[] charsOfAllowed = allowed.toCharArray(); - for (char c : charsOfAllowed) { - setChars.add(c); - } - // System.out.println(setChars.size()); - int res = 0; - for (String word : words) { - boolean isConsistent = true; - char[] charsOfWord = word.toCharArray(); - for (char c : charsOfWord) { - if (!setChars.contains(c)) { - isConsistent = false; - break; - } - } - if (isConsistent) { - res++; - } - } - return res; - } - - public static void main(String[] args) { - String allowed = "ab"; - String[] words = new String[]{"ad", "bd", "aaab", "baa", "badab"}; - // 2 - System.out.println(new CountTheNumberOfConsistentStrings1684().countConsistentStrings(allowed, words)); - } -} diff --git a/src/main/java/com/leetcode/easy/CountTheNumberOfConsistentStrings1684.java b/src/main/java/com/leetcode/easy/CountTheNumberOfConsistentStrings1684.java new file mode 100644 index 0000000..a6e3771 --- /dev/null +++ b/src/main/java/com/leetcode/easy/CountTheNumberOfConsistentStrings1684.java @@ -0,0 +1,47 @@ +// Tags: Array, String, Hash Set +package com.leetcode.easy; + +import java.util.HashSet; +import java.util.Set; + +public class CountTheNumberOfConsistentStrings1684 { + + /** + * Counts the number of consistent strings in the given array. + * A string is consistent if all its characters appear in the allowed string. + * + *

Algorithm: + *

    + *
  1. Convert allowed string to a HashSet for O(1) character lookup
  2. + *
  3. For each word, check if all characters exist in the allowed set
  4. + *
  5. Count words that pass the consistency check
  6. + *
+ * + *

Time Complexity: O(m + n×k) where m = allowed.length(), n = words.length, k = average word length + *
Space Complexity: O(m) for storing the allowed characters in HashSet + * + * @param allowed String containing all allowed characters + * @param words Array of strings to check for consistency + * @return The number of consistent strings + */ + public int countConsistentStrings(String allowed, String[] words) { + int res = 0; + Set allowedCharacters = new HashSet<>(); + for (int i = 0; i < allowed.length(); i++) { + allowedCharacters.add(allowed.charAt(i)); + } + for (String word : words) { + boolean isConsistent = true; + for (int i = 0; i < word.length(); i++) { + if (!allowedCharacters.contains(word.charAt(i))) { + isConsistent = false; + break; + } + } + if (isConsistent) { + res++; + } + } + return res; + } +} diff --git a/src/test/java/com/leetcode/easy/CountTheNumberOfConsistentStrings1684Test.java b/src/test/java/com/leetcode/easy/CountTheNumberOfConsistentStrings1684Test.java new file mode 100644 index 0000000..677bcbd --- /dev/null +++ b/src/test/java/com/leetcode/easy/CountTheNumberOfConsistentStrings1684Test.java @@ -0,0 +1,46 @@ +package com.leetcode.easy; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CountTheNumberOfConsistentStrings1684Test { + + @Test + void testExample1() { + CountTheNumberOfConsistentStrings1684 solution = new CountTheNumberOfConsistentStrings1684(); + String allowed = "ab"; + String[] words = {"ad", "bd", "aaab", "baa", "badab"}; + int expected = 2; + + int result = solution.countConsistentStrings(allowed, words); + + assertEquals(expected, result, + "Strings \"aaab\" and \"baa\" are consistent since they only contain characters 'a' and 'b'"); + } + + @Test + void testExample2() { + CountTheNumberOfConsistentStrings1684 solution = new CountTheNumberOfConsistentStrings1684(); + String allowed = "abc"; + String[] words = {"a", "b", "c", "ab", "ac", "bc", "abc"}; + int expected = 7; + + int result = solution.countConsistentStrings(allowed, words); + + assertEquals(expected, result, + "All strings should be consistent"); + } + + @Test + void testExample3() { + CountTheNumberOfConsistentStrings1684 solution = new CountTheNumberOfConsistentStrings1684(); + String allowed = "cad"; + String[] words = {"cc", "acd", "b", "ba", "bac", "bad", "ac", "d"}; + int expected = 4; + + int result = solution.countConsistentStrings(allowed, words); + + assertEquals(expected, result, + "Strings \"cc\", \"acd\", \"ac\", and \"d\" are consistent"); + } +}