From b424c76d8afadf1792f1afeaa1a342b6c2308aa0 Mon Sep 17 00:00:00 2001 From: Saurabh jangra <55055472+saurabh22111999@users.noreply.github.com> Date: Tue, 21 Oct 2025 17:07:38 +0000 Subject: [PATCH 1/2] Corrected typo and added optimization --- .../java/com/thealgorithms/strings/Upper.java | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/Upper.java b/src/main/java/com/thealgorithms/strings/Upper.java index 5e248cb6ee39..f38704d38939 100644 --- a/src/main/java/com/thealgorithms/strings/Upper.java +++ b/src/main/java/com/thealgorithms/strings/Upper.java @@ -1,6 +1,7 @@ package com.thealgorithms.strings; public final class Upper { + private Upper() { } @@ -15,25 +16,42 @@ public static void main(String[] args) { } /** - * Converts all the characters in this {@code String} to upper case + * Converts all the characters in this {@code String} to upper case. * * @param s the string to convert * @return the {@code String}, converted to uppercase. + * @throws IllegalArgumentException if {@code s} is null */ public static String toUpperCase(String s) { if (s == null) { - throw new IllegalArgumentException("Input string connot be null"); + throw new IllegalArgumentException("Input string cannot be null"); } if (s.isEmpty()) { return s; } - StringBuilder result = new StringBuilder(s); - for (int i = 0; i < result.length(); ++i) { - char currentChar = result.charAt(i); - if (Character.isLetter(currentChar) && Character.isLowerCase(currentChar)) { - result.setCharAt(i, Character.toUpperCase(currentChar)); + + // Check if any lowercase letter exists before creating a new String + boolean hasLower = false; + for (int i = 0; i < s.length(); i++) { + if (Character.isLowerCase(s.charAt(i))) { + hasLower = true; + break; } } - return result.toString(); + + // If no lowercase characters, return the same string + if (!hasLower) { + return s; + } + + // Convert lowercase letters to uppercase + char[] chars = s.toCharArray(); + for (int i = 0; i < chars.length; i++) { + if (Character.isLowerCase(chars[i])) { + chars[i] = Character.toUpperCase(chars[i]); + } + } + + return new String(chars); } } From b1c6cdac6d3ea66b8378b5aca2ff72b4765fa8db Mon Sep 17 00:00:00 2001 From: Saurabh jangra <55055472+saurabh22111999@users.noreply.github.com> Date: Tue, 21 Oct 2025 17:14:30 +0000 Subject: [PATCH 2/2] corrected typpo and addeed optimized code --- .../searches/TrappingRainWater.java | 53 +++++++++++++++++++ .../java/com/thealgorithms/strings/Upper.java | 20 +------ .../searches/TrappingRainWaterTest.java | 38 +++++++++++++ 3 files changed, 92 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/thealgorithms/searches/TrappingRainWater.java create mode 100644 src/test/java/com/thealgorithms/searches/TrappingRainWaterTest.java diff --git a/src/main/java/com/thealgorithms/searches/TrappingRainWater.java b/src/main/java/com/thealgorithms/searches/TrappingRainWater.java new file mode 100644 index 000000000000..48d9391dab97 --- /dev/null +++ b/src/main/java/com/thealgorithms/searches/TrappingRainWater.java @@ -0,0 +1,53 @@ +package com.thealgorithms.searches; + +/** + * Trapping Rain Water + * + * Given n non-negative integers representing an elevation map where the width of + * each bar is 1, compute how much water it can trap after raining. + * + * Approach: Two-pointer optimized O(n) time and O(1) extra space. + */ +public final class TrappingRainWater { + + private TrappingRainWater() {} + + /** + * Compute trapped rain water amount for the given heights array. + * + * @param height array of non-negative integers + * @return total units of trapped water + * @throws IllegalArgumentException if height is null + */ + public static int trap(final int[] height) { + if (height == null) { + throw new IllegalArgumentException("height array must not be null"); + } + + int left = 0; + int right = height.length - 1; + int leftMax = 0; + int rightMax = 0; + int trapped = 0; + + while (left <= right) { + if (height[left] <= height[right]) { + if (height[left] >= leftMax) { + leftMax = height[left]; + } else { + trapped += leftMax - height[left]; + } + left++; + } else { + if (height[right] >= rightMax) { + rightMax = height[right]; + } else { + trapped += rightMax - height[right]; + } + right--; + } + } + + return trapped; + } +} diff --git a/src/main/java/com/thealgorithms/strings/Upper.java b/src/main/java/com/thealgorithms/strings/Upper.java index f38704d38939..14e6a4909ca8 100644 --- a/src/main/java/com/thealgorithms/strings/Upper.java +++ b/src/main/java/com/thealgorithms/strings/Upper.java @@ -1,7 +1,6 @@ package com.thealgorithms.strings; public final class Upper { - private Upper() { } @@ -16,7 +15,7 @@ public static void main(String[] args) { } /** - * Converts all the characters in this {@code String} to upper case. + * Converts all the characters in this {@code String} to upper case * * @param s the string to convert * @return the {@code String}, converted to uppercase. @@ -29,29 +28,12 @@ public static String toUpperCase(String s) { if (s.isEmpty()) { return s; } - - // Check if any lowercase letter exists before creating a new String - boolean hasLower = false; - for (int i = 0; i < s.length(); i++) { - if (Character.isLowerCase(s.charAt(i))) { - hasLower = true; - break; - } - } - - // If no lowercase characters, return the same string - if (!hasLower) { - return s; - } - - // Convert lowercase letters to uppercase char[] chars = s.toCharArray(); for (int i = 0; i < chars.length; i++) { if (Character.isLowerCase(chars[i])) { chars[i] = Character.toUpperCase(chars[i]); } } - return new String(chars); } } diff --git a/src/test/java/com/thealgorithms/searches/TrappingRainWaterTest.java b/src/test/java/com/thealgorithms/searches/TrappingRainWaterTest.java new file mode 100644 index 000000000000..6891b3499da7 --- /dev/null +++ b/src/test/java/com/thealgorithms/searches/TrappingRainWaterTest.java @@ -0,0 +1,38 @@ +package com.thealgorithms.searches; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +public class TrappingRainWaterTest { + + @Test + public void testExample() { + int[] height = {4, 2, 0, 3, 2, 5}; + assertEquals(9, TrappingRainWater.trap(height)); + } + + @Test + public void testEmpty() { + int[] height = {}; + assertEquals(0, TrappingRainWater.trap(height)); + } + + @Test + public void testNoTrapping() { + int[] height = {0, 1, 2, 3, 4}; + assertEquals(0, TrappingRainWater.trap(height)); + } + + @Test + public void testFlat() { + int[] height = {3, 3, 3, 3}; + assertEquals(0, TrappingRainWater.trap(height)); + } + + @Test + public void testNull() { + assertThrows(IllegalArgumentException.class, () -> TrappingRainWater.trap(null)); + } +}