Skip to content

Commit e20a4ea

Browse files
Merge branch 'master' into refactor/longest_common_prefix
2 parents 5fee006 + 3e0fd11 commit e20a4ea

File tree

4 files changed

+80
-96
lines changed

4 files changed

+80
-96
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/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() {

0 commit comments

Comments
 (0)