Skip to content

Commit 696ce9d

Browse files
committed
Add 5 more solutions for array/string problems and 4 solutions for common two-pointers problems
1 parent a8722c0 commit 696ce9d

13 files changed

+301
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
out
33
src/**/*.md
4+
tmp

.run/Solution.run.xml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="CanPlaceFlowers" type="Application" factoryName="Application" nameIsGenerated="true">
3+
<option name="MAIN_CLASS_NAME" value="array_string.CanPlaceFlowers" />
4+
<module name="leetcode-75-qs-solutions-java" />
5+
<option name="VM_PARAMETERS" value="-ea" />
6+
<extension name="coverage">
7+
<pattern>
8+
<option name="PATTERN" value="array_string.*" />
9+
<option name="ENABLED" value="true" />
10+
</pattern>
11+
</extension>
12+
<method v="2">
13+
<option name="Make" enabled="true" />
14+
</method>
15+
</configuration>
16+
</component>

.vscode/launch.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"type": "java",
6+
"name": "Current File",
7+
"request": "launch",
8+
"mainClass": "${file}",
9+
"vmArgs": "-ea"
10+
}
11+
]
12+
}

README.md

+24-11
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,23 @@ leetcode-75-solutions-java/
6565

6666
| Category | Problem | Solution | Difficulty |
6767
|----------------|---------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------|------------|
68-
| Array / String | [Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately/) | [MergeStringsAlternately.java](src/array_string/MergeStringsAlternately.java) | Easy | |
69-
| Array / String | [Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/) | [GreatestCommonDivisorOfStrings.java](src/array_string/GreatestCommonDivisorOfStrings.java) | Easy | |
70-
| Array / String | [Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/) | [GreatestNumberOfCandies.java](src/array_string/GreatestNumberOfCandies.java) | Easy | |
71-
| Array / String | [Can Place Flowers](https://leetcode.com/problems/can-place-flowers/) | [CanPlaceFlowers.java](src/array_string/CanPlaceFlowers.java) | Easy | |
72-
| Array / String | [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | [ReverseVowelsOfString.java](src/array_string/ReverseVowelsOfString.java) | Easy | |
73-
| Array / String | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [ReverseWordsInString.java](src/array_string/ReverseWordsInString.java) | Medium | |
74-
| ... | ... | ... | ... | |
75-
| Trie | [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree) | [PrefixTrie.java](src/trie/PrefixTrie.java) | Medium | |
76-
| Trie | [Search Suggestions System](https://leetcode.com/problems/search-suggestions-system) | [SearchSuggestionsSystem.java](src/trie/SearchSuggestionsSystem.java) | Medium | |
77-
| ... | ... | ... | ... | |
68+
| Array / String | [Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately/) | [MergeStringsAlternately.java](src/array_string/MergeStringsAlternately.java) | Easy |
69+
| Array / String | [Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/) | [GreatestCommonDivisorOfStrings.java](src/array_string/GreatestCommonDivisorOfStrings.java) | Easy |
70+
| Array / String | [Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/) | [GreatestNumberOfCandies.java](src/array_string/GreatestNumberOfCandies.java) | Easy |
71+
| Array / String | [Can Place Flowers](https://leetcode.com/problems/can-place-flowers/) | [CanPlaceFlowers.java](src/array_string/CanPlaceFlowers.java) | Easy |
72+
| Array / String | [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | [ReverseVowelsOfString.java](src/array_string/ReverseVowelsOfString.java) | Easy |
73+
| Array / String | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [ReverseWordsInString.java](src/array_string/ReverseWordsInString.java) | Medium |
74+
| Array / String | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [ProductExceptSelf.java](src/array_string/ProductExceptSelf.java) | Medium |
75+
| Array / String | [Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [IncreasingTriplet.java](src/array_string/IncreasingTriplet.java) | Medium |
76+
| Array / String | [String Compression](https://leetcode.com/problems/string-compression/) | [StringCompressor.java](src/array_string/StringCompressor.java) | Medium |
77+
| Two Pointers | [Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [ZeroMover.java](src/two_pointers/ZeroMover.java) | Easy |
78+
| Two Pointers | [Is Subsequence](https://leetcode.com/problems/is-subsequence/) | [SubsequenceChecker.java](src/two_pointers/SubsequenceChecker.java) | Easy |
79+
| Two Pointers | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [WaterContainer.java](src/two_pointers/WaterContainer.java) | Medium |
80+
| Two Pointers | [Max Number of K-Sum Pairs](https://leetcode.com/problems/max-number-of-k-sum-pairs/) | [MaxOperationsCounter.java](src/two_pointers/MaxOperationsCounter.java) | Medium |
81+
| ... | ... | ... | ... |
82+
| Trie | [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree) | [PrefixTrie.java](src/trie/PrefixTrie.java) | Medium |
83+
| Trie | [Search Suggestions System](https://leetcode.com/problems/search-suggestions-system) | [SearchSuggestionsSystem.java](src/trie/SearchSuggestionsSystem.java) | Medium |
84+
| ... | ... | ... | ... |
7885

7986
## Usage
8087

@@ -98,7 +105,13 @@ javac ProblemName.java
98105
java -ea ProblemName
99106
```
100107

101-
If using IDEA, right-click the src folder, then select "Mark Directory" as "Sources Root". Now, Java files should benefit from all the Java IDE features of IntelliJ IDEA, including autocomplete, syntax highlighting, and more.
108+
or if you use Java11+ run the source code file directly, without intermediate compilation:
109+
110+
```bash
111+
java -ea ProblemName.java
112+
```
113+
114+
If using IDEA, right-click the src folder, then select **"Mark Directory"** as **"Sources Root"**. Now, Java files should benefit from all the Java IDE features of IntelliJ IDEA, including autocomplete, debug, syntax highlighting, and more.
102115

103116
Remember, when using assertions in Java, ensure that you've enabled them by using the `-ea` flag when running the Java program.
104117

src/array_string/CanPlaceFlowers.java

+1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ public static void main(String[] args) {
3030

3131
System.out.println("All tests passed!");
3232
}
33+
3334
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package array_string;
2+
3+
public class IncreasingTriplet {
4+
5+
public boolean hasIncreasingTriplet(int[] numbers) {
6+
int firstMin = Integer.MAX_VALUE, secondMin = Integer.MAX_VALUE;
7+
for (int number : numbers) {
8+
if (number <= firstMin) {
9+
firstMin = number;
10+
} else if (number <= secondMin) {
11+
secondMin = number;
12+
} else {
13+
return true;
14+
}
15+
}
16+
return false;
17+
}
18+
19+
public static void main(String[] args) {
20+
21+
IncreasingTriplet checker = new IncreasingTriplet();
22+
23+
int[] testInput1 = {1,2,3,4,5};
24+
assert checker.hasIncreasingTriplet(testInput1) == true : "Test case 1 failed";
25+
26+
int[] testInput2 = {5,4,3,2,1};
27+
assert checker.hasIncreasingTriplet(testInput2) == false : "Test case 2 failed";
28+
29+
int[] testInput3 = {2,1,5,0,4,6};
30+
assert checker.hasIncreasingTriplet(testInput3) == true : "Test case 3 failed";
31+
32+
System.out.println("All test cases passed!");
33+
}
34+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package array_string;
2+
3+
public class ProductExceptSelf {
4+
5+
public int[] calculateProduct(int[] nums) {
6+
int length = nums.length;
7+
int[] result = new int[length];
8+
9+
int leftMultiplier = 1;
10+
for (int idx = 0; idx < length; idx++) {
11+
if (idx > 0) {
12+
leftMultiplier = leftMultiplier * nums[idx - 1];
13+
}
14+
result[idx] = leftMultiplier;
15+
}
16+
17+
int rightMultiplier = 1;
18+
for (int idx = length - 1; idx >= 0; idx--) {
19+
if (idx < length - 1) {
20+
rightMultiplier = rightMultiplier * nums[idx + 1];
21+
}
22+
result[idx] *= rightMultiplier;
23+
}
24+
25+
return result;
26+
}
27+
28+
public static void main(String[] args) {
29+
30+
ProductExceptSelf calculator = new ProductExceptSelf();
31+
32+
int[] testInput1 = {1,2,3,4};
33+
int[] expectedOutput1 = {24,12,8,6};
34+
assert java.util.Arrays.equals(calculator.calculateProduct(testInput1), expectedOutput1) : "Test case 1 failed";
35+
36+
int[] testInput2 = {-1,1,0,-3,3};
37+
int[] expectedOutput2 = {0,0,9,0,0};
38+
assert java.util.Arrays.equals(calculator.calculateProduct(testInput2), expectedOutput2) : "Test case 2 failed";
39+
40+
System.out.println("All test cases passed!");
41+
}
42+
}

src/array_string/ReverseWordsInString.java

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public String reverseWords(String input) {
2323
public static void main(String[] args) {
2424
ReverseWordsInString reverser = new ReverseWordsInString();
2525

26-
// Sample assertions
2726
assert reverser.reverseWords("the sky is blue").equals("blue is sky the") : "Test case 1 failed";
2827
assert reverser.reverseWords(" hello world ").equals("world hello") : "Test case 2 failed";
2928
assert reverser.reverseWords("a good example").equals("example good a") : "Test case 3 failed";
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package array_string;
2+
3+
public class StringCompressor {
4+
5+
public int compress(char[] chars) {
6+
int writeIdx = 0; // index to write the compressed result
7+
int readIdx = 0; // index to read from the original array
8+
while (readIdx < chars.length) {
9+
char current = chars[readIdx];
10+
int freq = 0;
11+
while (readIdx < chars.length && chars[readIdx] == current) {
12+
readIdx++;
13+
freq++;
14+
}
15+
chars[writeIdx++] = current;
16+
if (freq > 1) {
17+
for (char ch : Integer.toString(freq).toCharArray()) {
18+
chars[writeIdx++] = ch;
19+
}
20+
}
21+
}
22+
return writeIdx;
23+
}
24+
25+
public static void main(String[] args) {
26+
27+
StringCompressor compressor = new StringCompressor();
28+
29+
char[] testInput1 = {'a','a','b','b','c','c','c'};
30+
assert compressor.compress(testInput1) == 6 && new String(testInput1, 0, 6).equals("a2b2c3") : "Test case 1 failed";
31+
32+
char[] testInput2 = {'a'};
33+
assert compressor.compress(testInput2) == 1 && new String(testInput2, 0, 1).equals("a") : "Test case 2 failed";
34+
35+
char[] testInput3 = {'a','b','b','b','b','b','b','b','b','b','b','b','b'};
36+
assert compressor.compress(testInput3) == 4 && new String(testInput3, 0, 4).equals("ab12") : "Test case 3 failed";
37+
38+
System.out.println("All test cases passed!");
39+
}
40+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package two_pointers;
2+
3+
import java.util.HashMap;
4+
5+
public class MaxOperationsCounter {
6+
7+
public int maxOperations(int[] nums, int k) {
8+
HashMap<Integer, Integer> numberCount = new HashMap<>();
9+
int operations = 0;
10+
11+
for (int num : nums) {
12+
if (numberCount.getOrDefault(k - num, 0) > 0) {
13+
operations++;
14+
numberCount.put(k - num, numberCount.get(k - num) - 1);
15+
} else {
16+
numberCount.put(num, numberCount.getOrDefault(num, 0) + 1);
17+
}
18+
}
19+
20+
return operations;
21+
}
22+
23+
public static void main(String[] args) {
24+
25+
MaxOperationsCounter counter = new MaxOperationsCounter();
26+
27+
assert counter.maxOperations(new int[]{1,2,3,4}, 5) == 2 : "Test case 1 failed";
28+
assert counter.maxOperations(new int[]{3,1,3,4,3}, 6) == 1 : "Test case 2 failed";
29+
30+
System.out.println("All test cases passed!");
31+
}
32+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package two_pointers;
2+
3+
public class SubsequenceChecker {
4+
5+
public boolean isSubsequence(String sub, String mainStr) {
6+
if (sub.isEmpty()) return true;
7+
int subIndex = 0, mainIndex = 0;
8+
9+
while (mainIndex < mainStr.length()) {
10+
if (mainStr.charAt(mainIndex) == sub.charAt(subIndex)) {
11+
subIndex++;
12+
if (subIndex == sub.length()) return true;
13+
}
14+
mainIndex++;
15+
}
16+
return false;
17+
}
18+
19+
public static void main(String[] args) {
20+
21+
SubsequenceChecker checker = new SubsequenceChecker();
22+
23+
assert checker.isSubsequence("abc", "ahbgdc") : "Test case 1 failed";
24+
assert !checker.isSubsequence("axc", "ahbgdc") : "Test case 2 failed";
25+
26+
System.out.println("All test cases passed!");
27+
}
28+
}

src/two_pointers/WaterContainer.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package two_pointers;
2+
3+
public class WaterContainer {
4+
5+
public int maxArea(int[] heights) {
6+
int start = 0;
7+
int end = heights.length - 1;
8+
int maxVolume = 0;
9+
10+
while(start < end) {
11+
int width = end - start;
12+
int height = Math.min(heights[start], heights[end]);
13+
int volume = height * width;
14+
15+
maxVolume = Math.max(maxVolume, volume);
16+
17+
if(heights[start] < heights[end]) {
18+
start++;
19+
} else {
20+
end--;
21+
}
22+
}
23+
24+
return maxVolume;
25+
}
26+
27+
public static void main(String[] args) {
28+
29+
WaterContainer container = new WaterContainer();
30+
31+
assert container.maxArea(new int[]{1,8,6,2,5,4,8,3,7}) == 49 : "Test case 1 failed";
32+
assert container.maxArea(new int[]{1,1}) == 1 : "Test case 2 failed";
33+
34+
System.out.println("All test cases passed!");
35+
}
36+
}

src/two_pointers/ZeroMover.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package two_pointers;
2+
3+
public class ZeroMover {
4+
5+
public void moveZeroes(int[] arr) {
6+
int nonZeroIndex = 0;
7+
8+
for (int num : arr) {
9+
if (num != 0) {
10+
arr[nonZeroIndex] = num;
11+
nonZeroIndex++;
12+
}
13+
}
14+
15+
while (nonZeroIndex < arr.length) {
16+
arr[nonZeroIndex] = 0;
17+
nonZeroIndex++;
18+
}
19+
}
20+
21+
public static void main(String[] args) {
22+
23+
ZeroMover mover = new ZeroMover();
24+
25+
int[] testInput1 = {0,1,0,3,12};
26+
mover.moveZeroes(testInput1);
27+
assert java.util.Arrays.equals(testInput1, new int[]{1,3,12,0,0}) : "Test case 1 failed";
28+
29+
int[] testInput2 = {0};
30+
mover.moveZeroes(testInput2);
31+
assert java.util.Arrays.equals(testInput2, new int[]{0}) : "Test case 2 failed";
32+
33+
System.out.println("All test cases passed!");
34+
}
35+
}

0 commit comments

Comments
 (0)