Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down
3 changes: 2 additions & 1 deletion docs/EASY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Easy Problems

Total: 38 problems solved
Total: 39 problems solved

## Solutions

Expand Down Expand Up @@ -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) |
Expand Down
35 changes: 0 additions & 35 deletions src/CountTheNumberOfConsistentStrings1684.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.
*
* <p>Algorithm:
* <ol>
* <li>Convert allowed string to a HashSet for O(1) character lookup</li>
* <li>For each word, check if all characters exist in the allowed set</li>
* <li>Count words that pass the consistency check</li>
* </ol>
*
* <p>Time Complexity: O(m + n×k) where m = allowed.length(), n = words.length, k = average word length
* <br>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<Character> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Loading