Skip to content

Commit 33bcd3b

Browse files
authored
Merge branch 'master' into refactor/PostfixToInfix
2 parents 794ab55 + e3ad376 commit 33bcd3b

File tree

8 files changed

+129
-126
lines changed

8 files changed

+129
-126
lines changed
Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,34 @@
11
package com.thealgorithms.stacks;
22

3-
import java.util.Arrays;
43
import java.util.Stack;
54

6-
/*
7-
Given an array "input" you need to print the first greater element for each element.
8-
For a given element x of an array, the Next greater element of that element is the
9-
first greater element to the right side of it. If no such element is present print -1.
10-
11-
Example
12-
input = { 2, 7, 3, 5, 4, 6, 8 };
13-
At i = 0
14-
Next greater element between (1 to n) is 7
15-
At i = 1
16-
Next greater element between (2 to n) is 8
17-
At i = 2
18-
Next greater element between (3 to n) is 5
19-
At i = 3
20-
Next greater element between (4 to n) is 6
21-
At i = 4
22-
Next greater element between (5 to n) is 6
23-
At i = 5
24-
Next greater element between (6 to n) is 8
25-
At i = 6
26-
Next greater element between (6 to n) is -1
27-
28-
result : [7, 8, 5, 6, 6, 8, -1]
29-
30-
1. If the stack is empty Push an element in the stack.
31-
2. If the stack is not empty:
32-
a. compare the top element of the stack with next.
33-
b. If next is greater than the top element, Pop element from the stack.
34-
next is the next greater element for the popped element.
35-
c. Keep popping from the stack while the popped element is smaller
36-
than next. next becomes the next greater element for all such
37-
popped elements.
38-
d. Finally, push the next in the stack.
39-
40-
3. If elements are left in stack after completing while loop then their Next greater element is
41-
-1.
5+
/**
6+
* Utility class to find the next greater element for each element in a given integer array.
7+
*
8+
* <p>The next greater element for an element x is the first greater element on the right side of x in the array.
9+
* If no such element exists, the result will contain 0 for that position.</p>
10+
*
11+
* <p>Example:</p>
12+
* <pre>
13+
* Input: {2, 7, 3, 5, 4, 6, 8}
14+
* Output: {7, 0, 5, 6, 6, 8, 0}
15+
* </pre>
4216
*/
43-
4417
public final class NextGreaterElement {
4518
private NextGreaterElement() {
4619
}
4720

21+
/**
22+
* Finds the next greater element for each element in the given array.
23+
*
24+
* @param array the input array of integers
25+
* @return an array where each element is replaced by the next greater element on the right side in the input array,
26+
* or 0 if there is no greater element.
27+
* @throws IllegalArgumentException if the input array is null
28+
*/
4829
public static int[] findNextGreaterElements(int[] array) {
4930
if (array == null) {
50-
return array;
31+
throw new IllegalArgumentException("Input array cannot be null");
5132
}
5233

5334
int[] result = new int[array.length];
@@ -62,10 +43,4 @@ public static int[] findNextGreaterElements(int[] array) {
6243

6344
return result;
6445
}
65-
66-
public static void main(String[] args) {
67-
int[] input = {2, 7, 3, 5, 4, 6, 8};
68-
int[] result = findNextGreaterElements(input);
69-
System.out.println(Arrays.toString(result));
70-
}
7146
}

src/main/java/com/thealgorithms/stacks/NextSmallerElement.java

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,57 @@
33
import java.util.Arrays;
44
import java.util.Stack;
55

6-
/*
7-
Given an array "input" you need to print the first smaller element for each element to the left
8-
side of an array. For a given element x of an array, the Next Smaller element of that element is
9-
the first smaller element to the left side of it. If no such element is present print -1.
10-
11-
Example
12-
input = { 2, 7, 3, 5, 4, 6, 8 };
13-
At i = 0
14-
No elements to left of it : -1
15-
At i = 1
16-
Next smaller element between (0 , 0) is 2
17-
At i = 2
18-
Next smaller element between (0 , 1) is 2
19-
At i = 3
20-
Next smaller element between (0 , 2) is 3
21-
At i = 4
22-
Next smaller element between (0 , 3) is 3
23-
At i = 5
24-
Next smaller element between (0 , 4) is 4
25-
At i = 6
26-
Next smaller element between (0 , 5) is 6
27-
28-
result : [-1, 2, 2, 3, 3, 4, 6]
29-
30-
1) Create a new empty stack st
31-
32-
2) Iterate over array "input" , where "i" goes from 0 to input.length -1.
33-
a) We are looking for value just smaller than `input[i]`. So keep popping from "stack"
34-
till elements in "stack.peek() >= input[i]" or stack becomes empty.
35-
b) If the stack is non-empty, then the top element is our previous element. Else the
36-
previous element does not exist. c) push input[i] in stack. 3) If elements are left then their
37-
answer is -1
6+
/**
7+
* Utility class to find the next smaller element for each element in a given integer array.
8+
*
9+
* <p>The next smaller element for an element x is the first smaller element on the left side of x in the array.
10+
* If no such element exists, the result will contain -1 for that position.</p>
11+
*
12+
* <p>Example:</p>
13+
* <pre>
14+
* Input: {2, 7, 3, 5, 4, 6, 8}
15+
* Output: [-1, 2, 2, 3, 3, 4, 6]
16+
* </pre>
3817
*/
39-
4018
public final class NextSmallerElement {
4119
private NextSmallerElement() {
4220
}
4321

22+
/**
23+
* Finds the next smaller element for each element in the given array.
24+
*
25+
* @param array the input array of integers
26+
* @return an array where each element is replaced by the next smaller element on the left side in the input array,
27+
* or -1 if there is no smaller element.
28+
* @throws IllegalArgumentException if the input array is null
29+
*/
4430
public static int[] findNextSmallerElements(int[] array) {
45-
// base case
4631
if (array == null) {
47-
return array;
32+
throw new IllegalArgumentException("Input array cannot be null");
4833
}
49-
Stack<Integer> stack = new Stack<>();
34+
5035
int[] result = new int[array.length];
36+
Stack<Integer> stack = new Stack<>();
37+
38+
// Initialize all elements to -1 (in case there is no smaller element)
5139
Arrays.fill(result, -1);
5240

41+
// Traverse the array from left to right
5342
for (int i = 0; i < array.length; i++) {
54-
while (!stack.empty() && stack.peek() >= array[i]) {
43+
// Maintain the stack such that the top of the stack is the next smaller element
44+
while (!stack.isEmpty() && stack.peek() >= array[i]) {
5545
stack.pop();
5646
}
57-
if (stack.empty()) {
58-
result[i] = -1;
59-
} else {
47+
48+
// If stack is not empty, then the top is the next smaller element
49+
if (!stack.isEmpty()) {
6050
result[i] = stack.peek();
6151
}
52+
53+
// Push the current element onto the stack
6254
stack.push(array[i]);
6355
}
64-
return result;
65-
}
6656

67-
public static void main(String[] args) {
68-
int[] input = {2, 7, 3, 5, 4, 6, 8};
69-
int[] result = findNextSmallerElements(input);
70-
System.out.println(Arrays.toString(result));
57+
return result;
7158
}
7259
}

src/main/java/com/thealgorithms/strings/ReverseStringRecursive.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/**
44
* Reverse String using Recursion
55
*/
6-
76
public final class ReverseStringRecursive {
87
private ReverseStringRecursive() {
98
}
9+
1010
/**
1111
* @param str string to be reversed
1212
* @return reversed string

src/main/java/com/thealgorithms/strings/StringCompression.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.thealgorithms.strings;
2-
/* References : https://en.wikipedia.org/wiki/Run-length_encoding
3-
* String compression algorithm deals with encoding the string, that is, shortening the size of the
4-
* string
2+
3+
/**
4+
* References : https://en.wikipedia.org/wiki/Run-length_encoding
5+
* String compression algorithm deals with encoding the string, that is, shortening the size of the string
56
* @author Swarga-codes (https://github.com/Swarga-codes)
67
*/
78
public final class StringCompression {
@@ -14,19 +15,16 @@ private StringCompression() {
1415
* @return the compressed character array as string
1516
*/
1617
public static String compress(String input) {
17-
// Keeping the count as 1 since every element present will have atleast a count
18-
// of 1
18+
// Keeping the count as 1 since every element present will have at least a count of 1
1919
int count = 1;
2020
String compressedString = "";
21-
// Base condition to check whether the array is of size 1, if it is then we
22-
// return the array
21+
// Base condition to check whether the array is of size 1, if it is then we return the array
2322
if (input.length() == 1) {
2423
return "" + input.charAt(0);
2524
}
2625
// If the array has a length greater than 1 we move into this loop
2726
for (int i = 0; i < input.length() - 1; i++) {
28-
// here we check for similarity of the adjacent elements and change the count
29-
// accordingly
27+
// here we check for similarity of the adjacent elements and change the count accordingly
3028
if (input.charAt(i) == input.charAt(i + 1)) {
3129
count = count + 1;
3230
}
@@ -54,7 +52,6 @@ public static String compress(String input) {
5452
public static String appendCount(String res, int count, char ch) {
5553
if (count > 1) {
5654
res += ch + "" + count;
57-
count = 1;
5855
} else {
5956
res += ch + "";
6057
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
class NextGreaterElementTest {
13+
14+
@ParameterizedTest
15+
@MethodSource("provideTestCases")
16+
void testFindNextGreaterElements(int[] input, int[] expected) {
17+
assertArrayEquals(expected, NextGreaterElement.findNextGreaterElements(input));
18+
}
19+
20+
static Stream<Arguments> provideTestCases() {
21+
return Stream.of(Arguments.of(new int[] {2, 7, 3, 5, 4, 6, 8}, new int[] {7, 8, 5, 6, 6, 8, 0}), Arguments.of(new int[] {5}, new int[] {0}), Arguments.of(new int[] {1, 2, 3, 4, 5}, new int[] {2, 3, 4, 5, 0}), Arguments.of(new int[] {5, 4, 3, 2, 1}, new int[] {0, 0, 0, 0, 0}),
22+
Arguments.of(new int[] {4, 5, 2, 25}, new int[] {5, 25, 25, 0}), Arguments.of(new int[] {}, new int[] {}));
23+
}
24+
25+
@Test
26+
void testNullInput() {
27+
assertThrows(IllegalArgumentException.class, () -> NextGreaterElement.findNextGreaterElements(null));
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
class NextSmallerElementTest {
13+
14+
@ParameterizedTest
15+
@MethodSource("provideTestCases")
16+
void testFindNextSmallerElements(int[] input, int[] expected) {
17+
assertArrayEquals(expected, NextSmallerElement.findNextSmallerElements(input));
18+
}
19+
20+
static Stream<Arguments> provideTestCases() {
21+
return Stream.of(Arguments.of(new int[] {2, 7, 3, 5, 4, 6, 8}, new int[] {-1, 2, 2, 3, 3, 4, 6}), Arguments.of(new int[] {5}, new int[] {-1}), Arguments.of(new int[] {1, 2, 3, 4, 5}, new int[] {-1, 1, 2, 3, 4}), Arguments.of(new int[] {5, 4, 3, 2, 1}, new int[] {-1, -1, -1, -1, -1}),
22+
Arguments.of(new int[] {4, 5, 2, 25}, new int[] {-1, 4, -1, 2}), Arguments.of(new int[] {}, new int[] {}));
23+
}
24+
25+
@Test
26+
void testFindNextSmallerElementsExceptions() {
27+
assertThrows(IllegalArgumentException.class, () -> NextSmallerElement.findNextSmallerElements(null));
28+
}
29+
}

src/test/java/com/thealgorithms/strings/ReverseStringRecursiveTest.java

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,15 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
67

78
public class ReverseStringRecursiveTest {
8-
@Test
9-
void shouldAcceptWhenEmptyStringIsPassed() {
10-
String expected = "";
11-
String reversed = ReverseStringRecursive.reverse("");
12-
13-
assertEquals(expected, reversed);
14-
}
15-
16-
@Test
17-
void shouldAcceptNotWhenWhenSingleCharacterIsPassed() {
18-
String expected = "a";
19-
String reversed = ReverseStringRecursive.reverse("a");
20-
21-
assertEquals(expected, reversed);
22-
}
23-
24-
@Test
25-
void shouldAcceptWhenStringIsPassed() {
26-
String expected = "dlroWolleH";
27-
String reversed = ReverseStringRecursive.reverse("HelloWorld");
28-
29-
assertEquals(expected, reversed);
9+
@ParameterizedTest
10+
@CsvSource({"'Hello World', 'dlroW olleH'", "'helloworld', 'dlrowolleh'", "'123456789', '987654321'", "'', ''", "'A', 'A'", "'!123 ABC xyz!', '!zyx CBA 321!'", "'Abc 123 Xyz', 'zyX 321 cbA'", "'12.34,56;78:90', '09:87;65,43.21'", "'abcdEFGHiJKL', 'LKJiHGFEdcba'",
11+
"'MixOf123AndText!', '!txeTdnA321fOxiM'"})
12+
public void
13+
testReverseString(String input, String expectedOutput) {
14+
assertEquals(expectedOutput, ReverseStringRecursive.reverse(input));
3015
}
3116
}

src/test/java/com/thealgorithms/strings/StringCompressionTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.thealgorithms.strings;
2+
23
import static org.junit.jupiter.api.Assertions.assertEquals;
34

45
import org.junit.jupiter.params.ParameterizedTest;
56
import org.junit.jupiter.params.provider.CsvSource;
67

78
public class StringCompressionTest {
89
@ParameterizedTest
9-
@CsvSource({"a,a", "aabbb,a2b3", "abbbc,ab3c", "aabccd,a2bc2d"})
10+
@CsvSource({"'a', 'a'", "'aabbb', 'a2b3'", "'abbbc', 'ab3c'", "'aabccd', 'a2bc2d'", "'aaaabbbcccc', 'a4b3c4'", "'abcd', 'abcd'", "'aabbccdd', 'a2b2c2d2'", "'aaabbaa', 'a3b2a2'", "'', ''", "'a', 'a'", "'aaaaa', 'a5'", "'aabb', 'a2b2'", "'aabbbaaa', 'a2b3a3'", "'qwerty', 'qwerty'"})
1011
void stringCompressionTest(String input, String expectedOutput) {
1112
String output = StringCompression.compress(input);
1213
assertEquals(expectedOutput, output);

0 commit comments

Comments
 (0)