From e0fa65c00d676f969964f9e8c4d1febbe98a6fd8 Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Wed, 10 Sep 2025 09:38:56 +0800 Subject: [PATCH] Add solution for the "Jewels and Stones" problem with unit tests. --- src/JewelsAndStones771.java | 26 ------------ .../com/leetcode/easy/JewelsAndStones771.java | 40 +++++++++++++++++++ .../leetcode/easy/JewelsAndStones771Test.java | 35 ++++++++++++++++ 3 files changed, 75 insertions(+), 26 deletions(-) delete mode 100644 src/JewelsAndStones771.java create mode 100644 src/main/java/com/leetcode/easy/JewelsAndStones771.java create mode 100644 src/test/java/com/leetcode/easy/JewelsAndStones771Test.java diff --git a/src/JewelsAndStones771.java b/src/JewelsAndStones771.java deleted file mode 100644 index abd5028..0000000 --- a/src/JewelsAndStones771.java +++ /dev/null @@ -1,26 +0,0 @@ -import java.util.HashSet; -import java.util.Set; - -public class JewelsAndStones771 { - public int numJewelsInStones(String jewels, String stones) { - int res = 0; - Set jewelTypes = new HashSet<>(); - char[] jewelsChars = jewels.toCharArray(); - for (char jewel : jewelsChars) { - jewelTypes.add(jewel); - } - char[] stonesChars = stones.toCharArray(); - for (char stone : stonesChars) { - if (jewelTypes.contains(stone)) { - res++; - } - } - return res; - } - - public static void main(String[] args) { - String jewels = "aA"; - String stones = "aAAbbbb"; - System.out.println(new JewelsAndStones771().numJewelsInStones(jewels, stones)); // 3 - } -} diff --git a/src/main/java/com/leetcode/easy/JewelsAndStones771.java b/src/main/java/com/leetcode/easy/JewelsAndStones771.java new file mode 100644 index 0000000..6bad16d --- /dev/null +++ b/src/main/java/com/leetcode/easy/JewelsAndStones771.java @@ -0,0 +1,40 @@ +// Tags: String, Hash Set +package com.leetcode.easy; + +import java.util.HashSet; +import java.util.Set; + +public class JewelsAndStones771 { + /* + Time complexity: O(J + S) + Breakdown: + - First loop: Iterates through the jewels string of length J, adding each character to the HashSet + - Each add() operation on HashSet takes O(1) average time + - Total: O(J) + - Second loop: Iterates through the stones string of length S, checking if each character exists in the HashSet + - Each contains() operation on HashSet takes O(1) average time + - Total: O(S) + - Overall: O(J) + O(S) = O(J + S) + Where J is the length of the jewels string and S is the length of the stones string. + + Space complexity: O(J) + Breakdown: + - HashSet storage: The jewelsTypes HashSet stores at most J unique characters from the jewels string + - In the worst case, all characters in jewels are unique, requiring O(J) space + - Other variables: res and loop variables use O(1) constant space + - Overall: O(J) + */ + public int numJewelsInStones(String jewels, String stones) { + Set jewelsTypes = new HashSet<>(); + for (int i = 0; i < jewels.length(); i++) { + jewelsTypes.add(jewels.charAt(i)); + } + int res = 0; + for (int i = 0; i < stones.length(); i++) { + if (jewelsTypes.contains(stones.charAt(i))) { + res++; + } + } + return res; + } +} diff --git a/src/test/java/com/leetcode/easy/JewelsAndStones771Test.java b/src/test/java/com/leetcode/easy/JewelsAndStones771Test.java new file mode 100644 index 0000000..2387fab --- /dev/null +++ b/src/test/java/com/leetcode/easy/JewelsAndStones771Test.java @@ -0,0 +1,35 @@ +package com.leetcode.easy; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class JewelsAndStones771Test { + private JewelsAndStones771 solution; + + @BeforeEach + void setUp() { + solution = new JewelsAndStones771(); + } + + @Test + void testNumJewelsInStones_Example1() { + String jewels = "aA"; + String stones = "aAAbbbb"; + + int result = solution.numJewelsInStones(jewels, stones); + + assertEquals(3, result); + } + + @Test + void testNumJewelsInStones_Example2() { + String jewels = "z"; + String stones = "ZZ"; + + int result = solution.numJewelsInStones(jewels, stones); + + assertEquals(0, result); + } +}