From 1da84bbc2cf82bf1373ee72232c6d2d6ab51bdca Mon Sep 17 00:00:00 2001 From: Harshitha Thondalapally Date: Wed, 31 Dec 2025 17:54:57 -0500 Subject: [PATCH] Completed Hashing 2 --- src/BinarySubArray.java | 33 +++++++++++++++++++++++++++++++++ src/LongestPalindrome.java | 31 +++++++++++++++++++++++++++++++ src/SubArraySumK.java | 30 ++++++++++++++++++++++++++++++ src/TestHashing2.java | 24 ++++++++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 src/BinarySubArray.java create mode 100644 src/LongestPalindrome.java create mode 100644 src/SubArraySumK.java create mode 100644 src/TestHashing2.java diff --git a/src/BinarySubArray.java b/src/BinarySubArray.java new file mode 100644 index 00000000..2e63bd6f --- /dev/null +++ b/src/BinarySubArray.java @@ -0,0 +1,33 @@ +/* +Problem - Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. +Approach - We increment running sum for every 1 and decrement it for every 0. +If the same running sum appears again, it means the subarray in between is balanced. +Track the longest such subarray using a hashmap. +Time Complexity - O(n) +Space Complexity - O(n) + */ + +import java.util.HashMap; + +public class BinarySubArray { + public int findMaxLength(int[] nums) { + HashMap map = new HashMap<>(); + int maxLength = 0; + int rSum = 0; + map.put(0, -1); + for (int i = 0; i < nums.length; i++) { + int num = nums[i]; + // if the num is 0 decrease the rsum by 1 if 1 increase the rsum by 1 + rSum = (num ==0)? rSum+1:rSum-1; + if(map.containsKey(rSum)){ + maxLength = Math.max(maxLength, i-map.get(rSum)); + } + else { + map.put(rSum, i); + } + + } + return maxLength; + } + +} diff --git a/src/LongestPalindrome.java b/src/LongestPalindrome.java new file mode 100644 index 00000000..81fbd908 --- /dev/null +++ b/src/LongestPalindrome.java @@ -0,0 +1,31 @@ +/* +Problem - Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters. +We use a HashSet to track characters that haven't formed a pair yet. +When we find a pair, we add 2 to the count and remove that char from the set. +If anything is left in the set, we can put one in the center — so we add 1 to the total. +Time Complexity - O(n) +Space Complexity - O(1) + */ + + +import java.util.HashSet; + +public class LongestPalindrome { + public int longestPalindrome(String s) { + HashSet set = new HashSet<>(); + int count = 0; + for ( char c : s.toCharArray() ) { + // if the character is already present in the set make it pair by increasing the counter and remove mapping + if (set.contains(c)) { + count += 2; + set.remove(c); + } + // if not present in the set add it + else { + set.add(c); + } + } + if (set.size() > 0) return count + 1; + return count; + } +} diff --git a/src/SubArraySumK.java b/src/SubArraySumK.java new file mode 100644 index 00000000..eb023955 --- /dev/null +++ b/src/SubArraySumK.java @@ -0,0 +1,30 @@ +/* +Problem - Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k. +Approach - Update current_sum by adding the current value of the array to it +If curr_sum - k exists in the hash map, add its frequency (map[curr_sum - k]) to count +Add (curr_sum, freq) to the hash map. If the key is already present, increase its frequency; if not, set it to 1. +Time Complexity - O(n) - iterating over all the elements of an array +Space Complexity - O(n) + */ + +import java.util.HashMap; + +public class SubArraySumK { + public int subarraySum(int[] nums, int k) { + int count = 0; + int currentSum = 0; + HashMap map = new HashMap<>(); + map.put(0, 1); + for (int num : nums) { + // updating the running prefix sum by adding the current number + currentSum += num; + // if currentSum-k is present the map add its frequency value to the counter + if (map.containsKey(currentSum - k)) { + count += map.get(currentSum - k); + } + // store the currentSum value in the hash map, if the current sum is already present in the map increase it by 1 + map.put(currentSum, map.getOrDefault(currentSum, 0) + 1); + } + return count; + } +} diff --git a/src/TestHashing2.java b/src/TestHashing2.java new file mode 100644 index 00000000..3e0bcbbe --- /dev/null +++ b/src/TestHashing2.java @@ -0,0 +1,24 @@ +public class TestHashing2 { + public static void main(String[] args) { + SubArraySumK obj = new SubArraySumK(); + int [] nums1 = {1,1,1}; + int [] nums2 = {1,2,3}; + System.out.println("Total Number of subarrays whose sum equals to " + 2 + " is " + obj.subarraySum(nums1,2)); + System.out.println("Total Number of subarrays whose sum equals to " + 3 + " is " + obj.subarraySum(nums2,3)); + // Test longest palindrome + LongestPalindrome obj2 = new LongestPalindrome(); + String s = "abccccdd"; + String s2 = "a"; + System.out.println("length of the longest palindrome that can be built with those letters of the String " + s + " is "+ obj2.longestPalindrome(s)); + System.out.println("length of the longest palindrome that can be built with those letters of the String " + s2 + " is "+ obj2.longestPalindrome(s2)); + + // Test Contiguous Array + BinarySubArray obj3 = new BinarySubArray(); + int [] bs1 = {0,1}; + int [] bs2 = {0,1,0}; + int [] bs3 = {0,1,1,1,1,1,0,0,0}; + System.out.println("maximum length of a contiguous subarray with an equal number of 0 and 1 is " + obj3.findMaxLength(bs1)); + System.out.println("maximum length of a contiguous subarray with an equal number of 0 and 1 is " + obj3.findMaxLength(bs2)); + System.out.println("maximum length of a contiguous subarray with an equal number of 0 and 1 is " + obj3.findMaxLength(bs3)); + } +}