├── .gitignore
├── pom.xml
└── src
├── main
└── java
│ └── org
│ └── example
│ ├── L01_ArraysAndHashing
│ ├── Anagram.java
│ ├── ContainsDuplicate.java
│ └── TwoIntegerSum.java
│ └── Main.java
└── test
└── java
└── L01_ArraysAndHashingTests
├── AnagramTest.java
├── ContainsDuplicateTest.java
└── TwoIntegerSumTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 | !**/src/main/**/target/
4 | !**/src/test/**/target/
5 |
6 | ### IntelliJ IDEA ###
7 | .idea/modules.xml
8 | .idea/jarRepositories.xml
9 | .idea/compiler.xml
10 | .idea/libraries/
11 | *.iws
12 | *.iml
13 | *.ipr
14 |
15 | ### Eclipse ###
16 | .apt_generated
17 | .classpath
18 | .factorypath
19 | .project
20 | .settings
21 | .springBeans
22 | .sts4-cache
23 |
24 | ### NetBeans ###
25 | /nbproject/private/
26 | /nbbuild/
27 | /dist/
28 | /nbdist/
29 | /.nb-gradle/
30 | build/
31 | !**/src/main/**/build/
32 | !**/src/test/**/build/
33 |
34 | ### VS Code ###
35 | .vscode/
36 |
37 | ### Mac OS ###
38 | .DS_Store
39 | /.idea/
40 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | org.example
8 | neetcode-blind-75-algo
9 | 1.0-SNAPSHOT
10 |
11 |
12 | 23
13 | 23
14 | UTF-8
15 |
16 |
17 |
18 |
19 | org.junit.jupiter
20 | junit-jupiter-api
21 | 5.11.2
22 | test
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/src/main/java/org/example/L01_ArraysAndHashing/Anagram.java:
--------------------------------------------------------------------------------
1 | package org.example.L01_ArraysAndHashing;
2 |
3 | public class Anagram {
4 | public boolean isAnagram(String s, String t) {
5 | if (s.length() != t.length()) return false;
6 |
7 | // Create an array to count the frequency of each character
8 | int[] counts = new int[26];
9 |
10 | // Increment for each character in 's' and decrement for each character in 't'
11 | for (int i = 0; i < s.length(); i++) {
12 | counts[s.charAt(i) - 'a']++;
13 | counts[t.charAt(i) - 'a']--;
14 | }
15 |
16 | // all counts should be 0 if strings are anagrams
17 | for (int i: counts) {
18 | if (i != 0) return false;
19 | }
20 | return true;
21 | }
22 | }
23 |
24 | /*
25 | Is Anagram
26 |
27 | Given two strings s and t, return true if the two strings are anagrams of each other, otherwise return false.
28 |
29 | An anagram is a string that contains the exact same characters as another string, but the order of the characters can be different.
30 |
31 | Example 1:
32 |
33 | Input: s = "racecar", t = "carrace"
34 |
35 | Output: true
36 |
37 | Example 2:
38 |
39 | Input: s = "jar", t = "jam"
40 |
41 | Output: false
42 |
43 | Constraints:
44 |
45 | s and t consist of lowercase English letters.
46 |
47 | */
--------------------------------------------------------------------------------
/src/main/java/org/example/L01_ArraysAndHashing/ContainsDuplicate.java:
--------------------------------------------------------------------------------
1 | package org.example.L01_ArraysAndHashing;
2 |
3 | import java.util.HashSet;
4 |
5 | public class ContainsDuplicate {
6 | public boolean checkDuplicates(int[] nums) {
7 | HashSet tempSet = new HashSet<>();
8 | for (int num : nums) {
9 | if (tempSet.contains(num)) {
10 | return true;
11 | }
12 | tempSet.add(num);
13 | }
14 | return false;
15 | }
16 | }
17 |
18 | /*
19 | Duplicate Integer
20 |
21 | Given an integer array nums, return true if any value appears more than once in the array, otherwise return false.
22 |
23 | Example 1:
24 |
25 | Input: nums = [1, 2, 3, 3]
26 |
27 | Output: true
28 |
29 | Example 2:
30 |
31 | Input: nums = [1, 2, 3, 4]
32 |
33 | Output: false
34 |
35 | */
--------------------------------------------------------------------------------
/src/main/java/org/example/L01_ArraysAndHashing/TwoIntegerSum.java:
--------------------------------------------------------------------------------
1 | package org.example.L01_ArraysAndHashing;
2 |
3 | import java.util.*;
4 |
5 | public class TwoIntegerSum {
6 | public int[] check(int[] nums, int target) {
7 | Map map = new HashMap<>();
8 |
9 | for (int i = 0; i < nums.length; i++) {
10 | int remainder = target - nums[i];
11 | if (map.containsKey(remainder)) {
12 | return new int[]{
13 | Math.min(map.get(remainder),i),
14 | Math.max(map.get(remainder),i)
15 | };
16 | }
17 | map.put(nums[i], i);
18 | }
19 |
20 | throw new IllegalArgumentException("No indices found");
21 | }
22 | }
23 |
24 | /*
25 | Given an array of integers nums and an integer target, return the indices i and j such that nums[i] + nums[j] == target and i != j.
26 |
27 | You may assume that every input has exactly one pair of indices i and j that satisfy the condition.
28 |
29 | Return the answer with the smaller index first.
30 |
31 | Example 1:
32 |
33 | Input:
34 | nums = [3,4,5,6], target = 7
35 |
36 | Output: [0,1]
37 |
38 | Explanation: nums[0] + nums[1] == 7, so we return [0, 1].
39 |
40 | Example 2:
41 |
42 | Input: nums = [4,5,6], target = 10
43 |
44 | Output: [0,2]
45 |
46 | Example 3:
47 |
48 | Input: nums = [5,5], target = 10
49 |
50 | Output: [0,1]
51 |
52 | Constraints:
53 |
54 | 2 <= nums.length <= 1000
55 | -10,000,000 <= nums[i] <= 10,000,000
56 | -10,000,000 <= target <= 10,000,000
57 |
58 |
59 | */
60 |
--------------------------------------------------------------------------------
/src/main/java/org/example/Main.java:
--------------------------------------------------------------------------------
1 | package org.example;
2 |
3 | public class Main {
4 | public static void main(String[] args) {
5 | System.out.println("Hello world!");
6 | }
7 | }
--------------------------------------------------------------------------------
/src/test/java/L01_ArraysAndHashingTests/AnagramTest.java:
--------------------------------------------------------------------------------
1 | package L01_ArraysAndHashingTests;
2 |
3 | import org.example.L01_ArraysAndHashing.Anagram;
4 | import org.junit.jupiter.api.Test;
5 |
6 | import static org.junit.jupiter.api.Assertions.assertFalse;
7 | import static org.junit.jupiter.api.Assertions.assertTrue;
8 |
9 | class AnagramTest {
10 | @Test
11 | void racecarAndCarraceReturnsTrue() {
12 | Anagram anagram = new Anagram();
13 | String[] testInput = {"racecar", "carrace"};
14 | assertTrue(anagram.isAnagram(testInput[0], testInput[1]));
15 | }
16 |
17 | @Test
18 | void jarAndJamReturnsFalse() {
19 | Anagram anagram = new Anagram();
20 | String[] testInput = {"jar", "jam"};
21 | assertFalse(anagram.isAnagram(testInput[0], testInput[1]));
22 | }
23 | }
--------------------------------------------------------------------------------
/src/test/java/L01_ArraysAndHashingTests/ContainsDuplicateTest.java:
--------------------------------------------------------------------------------
1 | package L01_ArraysAndHashingTests;
2 |
3 | import org.example.L01_ArraysAndHashing.ContainsDuplicate;
4 | import org.junit.jupiter.api.Test;
5 |
6 | import static org.junit.jupiter.api.Assertions.*;
7 |
8 | class ContainsDuplicateTest {
9 |
10 | @Test
11 | void containsDuplicateReturnsTrue() {
12 | ContainsDuplicate dup = new ContainsDuplicate();
13 | int[] testInput = {1,2,2,3};
14 | assertTrue(dup.checkDuplicates(testInput));
15 | }
16 |
17 | @Test
18 | void containsDuplicateReturnsFalse() {
19 | ContainsDuplicate dup = new ContainsDuplicate();
20 | int[] testInput = {1,2,3,4};
21 | assertFalse(dup.checkDuplicates(testInput));
22 | }
23 | }
--------------------------------------------------------------------------------
/src/test/java/L01_ArraysAndHashingTests/TwoIntegerSumTest.java:
--------------------------------------------------------------------------------
1 | package L01_ArraysAndHashingTests;
2 |
3 | import org.example.L01_ArraysAndHashing.TwoIntegerSum;
4 | import org.junit.jupiter.api.Test;
5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
6 |
7 | class TwoIntegerSumTest {
8 |
9 | @Test
10 | public void testFindTwoSum_ValidPair() {
11 | TwoIntegerSum twoIntegerSum = new TwoIntegerSum();
12 | assertArrayEquals(new int[] {0, 1}, twoIntegerSum.check(new int[] {3, 4, 5, 6}, 7));
13 | assertArrayEquals(new int[] {0, 2}, twoIntegerSum.check(new int[] {4, 5, 6}, 10));
14 | assertArrayEquals(new int[] {0, 1}, twoIntegerSum.check(new int[] {5, 5}, 10));
15 | }
16 |
17 | @Test
18 | public void testFindTwoSum_NegativeNumbers() {
19 | TwoIntegerSum twoIntegerSum = new TwoIntegerSum();
20 | assertArrayEquals(new int[] {1, 2}, twoIntegerSum.check(new int[] {1, -2, 3}, 1));
21 | }
22 |
23 | @Test
24 | public void testFindTwoSum_LargeNumbers() {
25 | TwoIntegerSum twoIntegerSum = new TwoIntegerSum();
26 | assertArrayEquals(new int[] {0, 1}, twoIntegerSum.check(new int[] {10000000, -10000000}, 0));
27 | }
28 |
29 | @Test
30 | public void testFindTwoSum_MinimumArrayLength() {
31 | TwoIntegerSum twoIntegerSum = new TwoIntegerSum();
32 | assertArrayEquals(new int[] {0, 1}, twoIntegerSum.check(new int[] {2, 3}, 5));
33 | }
34 | }
--------------------------------------------------------------------------------