Skip to content

Commit e700e39

Browse files
Merge branch 'master' into cleanup/RangeInSortedArray
2 parents df5d309 + 048bba9 commit e700e39

File tree

6 files changed

+118
-163
lines changed

6 files changed

+118
-163
lines changed

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

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,50 @@
33
import java.util.Stack;
44

55
/**
6+
* Utility class to calculate the largest rectangle area in a histogram.
7+
* Each bar's width is assumed to be 1 unit.
68
*
7-
* @author mohd rameez github.com/rameez471
9+
* <p>This implementation uses a monotonic stack to efficiently calculate
10+
* the area of the largest rectangle that can be formed from the histogram bars.</p>
11+
*
12+
* <p>Example usage:
13+
* <pre>{@code
14+
* int[] heights = {2, 1, 5, 6, 2, 3};
15+
* String area = LargestRectangle.largestRectangleHistogram(heights);
16+
* // area is "10"
17+
* }</pre>
818
*/
9-
1019
public final class LargestRectangle {
20+
1121
private LargestRectangle() {
1222
}
1323

24+
/**
25+
* Calculates the largest rectangle area in the given histogram.
26+
*
27+
* @param heights an array of non-negative integers representing bar heights
28+
* @return the largest rectangle area as a {@link String}
29+
*/
1430
public static String largestRectangleHistogram(int[] heights) {
15-
int n = heights.length;
1631
int maxArea = 0;
17-
Stack<int[]> st = new Stack<>();
18-
for (int i = 0; i < n; i++) {
32+
Stack<int[]> stack = new Stack<>();
33+
34+
for (int i = 0; i < heights.length; i++) {
1935
int start = i;
20-
while (!st.isEmpty() && st.peek()[1] > heights[i]) {
21-
int[] tmp = st.pop();
22-
maxArea = Math.max(maxArea, tmp[1] * (i - tmp[0]));
23-
start = tmp[0];
36+
while (!stack.isEmpty() && stack.peek()[1] > heights[i]) {
37+
int[] popped = stack.pop();
38+
maxArea = Math.max(maxArea, popped[1] * (i - popped[0]));
39+
start = popped[0];
2440
}
25-
st.push(new int[] {start, heights[i]});
41+
stack.push(new int[] {start, heights[i]});
2642
}
27-
while (!st.isEmpty()) {
28-
int[] tmp = st.pop();
29-
maxArea = Math.max(maxArea, tmp[1] * (n - tmp[0]));
43+
44+
int totalLength = heights.length;
45+
while (!stack.isEmpty()) {
46+
int[] remaining = stack.pop();
47+
maxArea = Math.max(maxArea, remaining[1] * (totalLength - remaining[0]));
3048
}
31-
return Integer.toString(maxArea);
32-
}
3349

34-
public static void main(String[] args) {
35-
assert largestRectangleHistogram(new int[] {2, 1, 5, 6, 2, 3}).equals("10");
36-
assert largestRectangleHistogram(new int[] {2, 4}).equals("4");
50+
return Integer.toString(maxArea);
3751
}
3852
}

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

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,54 @@
55
import java.util.Map;
66
import java.util.Set;
77

8+
/**
9+
* Utility class to check if two strings are isomorphic.
10+
*
11+
* <p>
12+
* Two strings {@code s} and {@code t} are isomorphic if the characters in {@code s}
13+
* can be replaced to get {@code t}, while preserving the order of characters.
14+
* Each character must map to exactly one character, and no two characters can map to the same character.
15+
* </p>
16+
*
17+
* @see <a href="https://en.wikipedia.org/wiki/Isomorphism_(computer_science)">Isomorphic Strings</a>
18+
*/
819
public final class Isomorphic {
20+
921
private Isomorphic() {
1022
}
1123

12-
public static boolean checkStrings(String s, String t) {
24+
/**
25+
* Checks if two strings are isomorphic.
26+
*
27+
* @param s the first input string
28+
* @param t the second input string
29+
* @return {@code true} if {@code s} and {@code t} are isomorphic; {@code false} otherwise
30+
*/
31+
public static boolean areIsomorphic(String s, String t) {
1332
if (s.length() != t.length()) {
1433
return false;
1534
}
1635

17-
// To mark the characters of string using MAP
18-
// character of first string as KEY and another as VALUE
19-
// now check occurence by keeping the track with SET data structure
20-
Map<Character, Character> characterMap = new HashMap<>();
21-
Set<Character> trackUniqueCharacter = new HashSet<>();
36+
Map<Character, Character> map = new HashMap<>();
37+
Set<Character> usedCharacters = new HashSet<>();
2238

2339
for (int i = 0; i < s.length(); i++) {
24-
if (characterMap.containsKey(s.charAt(i))) {
25-
if (t.charAt(i) != characterMap.get(s.charAt(i))) {
40+
char sourceChar = s.charAt(i);
41+
char targetChar = t.charAt(i);
42+
43+
if (map.containsKey(sourceChar)) {
44+
if (map.get(sourceChar) != targetChar) {
2645
return false;
2746
}
2847
} else {
29-
if (trackUniqueCharacter.contains(t.charAt(i))) {
48+
if (usedCharacters.contains(targetChar)) {
3049
return false;
3150
}
32-
33-
characterMap.put(s.charAt(i), t.charAt(i));
51+
map.put(sourceChar, targetChar);
52+
usedCharacters.add(targetChar);
3453
}
35-
trackUniqueCharacter.add(t.charAt(i));
3654
}
55+
3756
return true;
3857
}
3958
}

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,41 @@
22

33
import java.util.Arrays;
44

5+
/**
6+
* Utility class for string operations.
7+
* <p>
8+
* This class provides a method to find the longest common prefix (LCP)
9+
* among an array of strings.
10+
* </p>
11+
*
12+
* @see <a href="https://en.wikipedia.org/wiki/Longest_common_prefix">Longest Common Prefix - Wikipedia</a>
13+
*/
514
public final class LongestCommonPrefix {
6-
public String longestCommonPrefix(String[] strs) {
15+
16+
private LongestCommonPrefix() {
17+
}
18+
19+
/**
20+
* Finds the longest common prefix among a list of strings using lexicographical sorting.
21+
* The prefix is common to the first and last elements after sorting the array.
22+
*
23+
* @param strs array of input strings
24+
* @return the longest common prefix, or empty string if none exists
25+
*/
26+
public static String longestCommonPrefix(String[] strs) {
727
if (strs == null || strs.length == 0) {
828
return "";
929
}
1030

1131
Arrays.sort(strs);
12-
String shortest = strs[0];
13-
String longest = strs[strs.length - 1];
32+
String first = strs[0];
33+
String last = strs[strs.length - 1];
1434

1535
int index = 0;
16-
while (index < shortest.length() && index < longest.length() && shortest.charAt(index) == longest.charAt(index)) {
36+
while (index < first.length() && index < last.length() && first.charAt(index) == last.charAt(index)) {
1737
index++;
1838
}
1939

20-
return shortest.substring(0, index);
40+
return first.substring(0, index);
2141
}
2242
}

src/test/java/com/thealgorithms/stacks/LargestRectangleTest.java

Lines changed: 15 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,27 @@
22

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

5-
import org.junit.jupiter.api.Test;
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
69

710
public class LargestRectangleTest {
811

9-
@Test
10-
void testLargestRectangleHistogramWithTypicalCases() {
11-
// Typical case with mixed heights
12-
int[] heights = {2, 1, 5, 6, 2, 3};
13-
String expected = "10";
14-
String result = LargestRectangle.largestRectangleHistogram(heights);
15-
assertEquals(expected, result);
16-
17-
// Another typical case with increasing heights
18-
heights = new int[] {2, 4};
19-
expected = "4";
20-
result = LargestRectangle.largestRectangleHistogram(heights);
21-
assertEquals(expected, result);
22-
23-
// Case with multiple bars of the same height
24-
heights = new int[] {4, 4, 4, 4};
25-
expected = "16";
26-
result = LargestRectangle.largestRectangleHistogram(heights);
27-
assertEquals(expected, result);
12+
@ParameterizedTest(name = "Histogram: {0} → Expected area: {1}")
13+
@MethodSource("histogramProvider")
14+
void testLargestRectangleHistogram(int[] heights, String expected) {
15+
assertEquals(expected, LargestRectangle.largestRectangleHistogram(heights));
2816
}
2917

30-
@Test
31-
void testLargestRectangleHistogramWithEdgeCases() {
32-
// Edge case with an empty array
33-
int[] heights = {};
34-
String expected = "0";
35-
String result = LargestRectangle.largestRectangleHistogram(heights);
36-
assertEquals(expected, result);
37-
38-
// Edge case with a single bar
39-
heights = new int[] {5};
40-
expected = "5";
41-
result = LargestRectangle.largestRectangleHistogram(heights);
42-
assertEquals(expected, result);
43-
44-
// Edge case with all bars of height 0
45-
heights = new int[] {0, 0, 0};
46-
expected = "0";
47-
result = LargestRectangle.largestRectangleHistogram(heights);
48-
assertEquals(expected, result);
18+
static Stream<Arguments> histogramProvider() {
19+
return Stream.of(Arguments.of(new int[] {2, 1, 5, 6, 2, 3}, "10"), Arguments.of(new int[] {2, 4}, "4"), Arguments.of(new int[] {4, 4, 4, 4}, "16"), Arguments.of(new int[] {}, "0"), Arguments.of(new int[] {5}, "5"), Arguments.of(new int[] {0, 0, 0}, "0"),
20+
Arguments.of(new int[] {6, 2, 5, 4, 5, 1, 6}, "12"), Arguments.of(new int[] {2, 1, 5, 6, 2, 3, 1}, "10"), Arguments.of(createLargeArray(10000, 1), "10000"));
4921
}
5022

51-
@Test
52-
void testLargestRectangleHistogramWithLargeInput() {
53-
// Large input case
54-
int[] heights = new int[10000];
55-
for (int i = 0; i < heights.length; i++) {
56-
heights[i] = 1;
57-
}
58-
String expected = "10000";
59-
String result = LargestRectangle.largestRectangleHistogram(heights);
60-
assertEquals(expected, result);
61-
}
62-
63-
@Test
64-
void testLargestRectangleHistogramWithComplexCases() {
65-
// Complex case with a mix of heights
66-
int[] heights = {6, 2, 5, 4, 5, 1, 6};
67-
String expected = "12";
68-
String result = LargestRectangle.largestRectangleHistogram(heights);
69-
assertEquals(expected, result);
70-
71-
// Case with a peak in the middle
72-
heights = new int[] {2, 1, 5, 6, 2, 3, 1};
73-
expected = "10";
74-
result = LargestRectangle.largestRectangleHistogram(heights);
75-
assertEquals(expected, result);
23+
private static int[] createLargeArray(int size, int value) {
24+
int[] arr = new int[size];
25+
java.util.Arrays.fill(arr, value);
26+
return arr;
7627
}
7728
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public final class IsomorphicTest {
1212
@ParameterizedTest
1313
@MethodSource("inputs")
1414
public void testCheckStrings(String str1, String str2, Boolean expected) {
15-
assertEquals(expected, Isomorphic.checkStrings(str1, str2));
16-
assertEquals(expected, Isomorphic.checkStrings(str2, str1));
15+
assertEquals(expected, Isomorphic.areIsomorphic(str1, str2));
16+
assertEquals(expected, Isomorphic.areIsomorphic(str2, str1));
1717
}
1818

1919
private static Stream<Arguments> inputs() {

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

Lines changed: 13 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,23 @@
22

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

5-
import org.junit.jupiter.api.Test;
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
610

711
public class LongestCommonPrefixTest {
812

9-
private final LongestCommonPrefix longestCommonPrefix = new LongestCommonPrefix();
10-
11-
@Test
12-
public void testCommonPrefix() {
13-
String[] input = {"flower", "flow", "flight"};
14-
String expected = "fl";
15-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
16-
}
17-
18-
@Test
19-
public void testNoCommonPrefix() {
20-
String[] input = {"dog", "racecar", "car"};
21-
String expected = "";
22-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
23-
}
24-
25-
@Test
26-
public void testEmptyArray() {
27-
String[] input = {};
28-
String expected = "";
29-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
30-
}
31-
32-
@Test
33-
public void testNullArray() {
34-
String[] input = null;
35-
String expected = "";
36-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
37-
}
38-
39-
@Test
40-
public void testSingleString() {
41-
String[] input = {"single"};
42-
String expected = "single";
43-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
44-
}
45-
46-
@Test
47-
public void testCommonPrefixWithDifferentLengths() {
48-
String[] input = {"ab", "a"};
49-
String expected = "a";
50-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
51-
}
52-
53-
@Test
54-
public void testAllSameStrings() {
55-
String[] input = {"test", "test", "test"};
56-
String expected = "test";
57-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
58-
}
59-
60-
@Test
61-
public void testPrefixAtEnd() {
62-
String[] input = {"abcde", "abcfgh", "abcmnop"};
63-
String expected = "abc";
64-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
13+
@ParameterizedTest(name = "{index} => input={0}, expected=\"{1}\"")
14+
@MethodSource("provideTestCases")
15+
@DisplayName("Test Longest Common Prefix")
16+
void testLongestCommonPrefix(String[] input, String expected) {
17+
assertEquals(expected, LongestCommonPrefix.longestCommonPrefix(input));
6518
}
6619

67-
@Test
68-
public void testMixedCase() {
69-
String[] input = {"Flower", "flow", "flight"};
70-
String expected = "";
71-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
20+
private static Stream<Arguments> provideTestCases() {
21+
return Stream.of(Arguments.of(new String[] {"flower", "flow", "flight"}, "fl"), Arguments.of(new String[] {"dog", "racecar", "car"}, ""), Arguments.of(new String[] {}, ""), Arguments.of(null, ""), Arguments.of(new String[] {"single"}, "single"), Arguments.of(new String[] {"ab", "a"}, "a"),
22+
Arguments.of(new String[] {"test", "test", "test"}, "test"), Arguments.of(new String[] {"abcde", "abcfgh", "abcmnop"}, "abc"), Arguments.of(new String[] {"Flower", "flow", "flight"}, ""));
7223
}
7324
}

0 commit comments

Comments
 (0)