Skip to content

Commit 9783708

Browse files
committed
Finished new Leetcode problem
1 parent b0ce2c1 commit 9783708

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package problems.leetcode.hash_table;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class KthDistinctStringInArray {
7+
public String kthDistinct(String[] arr, int k) {
8+
Map<String, Boolean> map = new HashMap<String, Boolean>();
9+
for (String s : arr) {
10+
if (!map.containsKey(s))
11+
map.put(s, true);
12+
else
13+
map.put(s, false);
14+
}
15+
for (String s : arr) {
16+
if (map.get(s)) {
17+
k--;
18+
if (k == 0) return s;
19+
}
20+
}
21+
return "";
22+
}
23+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package problems.leetcode.hash_table;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
public class KthDistinctStringInArrayTest {
8+
9+
@Test
10+
void testSimple() {
11+
KthDistinctStringInArray kthDistinctStringInArray = new KthDistinctStringInArray();
12+
String[] input = {"d", "b", "c", "b", "c", "a"};
13+
String result = kthDistinctStringInArray.kthDistinct(input, 2);
14+
assertEquals("a", result);
15+
}
16+
17+
@Test
18+
void testMultipleSymbols() {
19+
KthDistinctStringInArray kthDistinctStringInArray = new KthDistinctStringInArray();
20+
String[] input = {"aaa", "aa", "a"};
21+
String result = kthDistinctStringInArray.kthDistinct(input, 1);
22+
assertEquals("aaa", result);
23+
}
24+
25+
@Test
26+
void testComplexString() {
27+
KthDistinctStringInArray kthDistinctStringInArray = new KthDistinctStringInArray();
28+
String[] input = {"a", "b", "a"};
29+
String result = kthDistinctStringInArray.kthDistinct(input, 3);
30+
assertEquals("", result);
31+
}
32+
}

Java/src/test/java/problems/leetcode/hash_table/RomanToIntegerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.junit.jupiter.api.Test;
44

5-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
65
import static org.junit.jupiter.api.Assertions.assertEquals;
76

87
class RomanToIntegerTest {

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Here is a list of the problems I've solved along with links to the corresponding
4343
| [3Sum](https://leetcode.com/problems/3sum/) | [Java](Java/src/main/java/problems/leetcode/two_pointers/ThreeSum.java) |
4444
| [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix) | [Java](Java/src/main/java/problems/leetcode/others/LongestCommonPrefix.java) |
4545
| [Roman to Integer](https://leetcode.com/problems/roman-to-integer) | [Java](Java/src/main/java/problems/leetcode/hash_table/RomanToInteger.java) |
46+
| [Roman to Integer](https://leetcode.com/problems/kth-distinct-string-in-an-array) | [Java](Java/src/main/java/problems/leetcode/hash_table/KthDistinctStringInArray.java)|
4647

4748
## Java Solutions
4849

0 commit comments

Comments
 (0)