From 00c361363af6a5c6092eb30daa31ea8748e243af Mon Sep 17 00:00:00 2001 From: sainathek Date: Mon, 12 Jan 2026 02:18:01 -0500 Subject: [PATCH] Done Hashing-2 --- problem1.java | 35 +++++++++++++++++++++++++++++++++++ problem2.java | 37 +++++++++++++++++++++++++++++++++++++ problem3.java | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 problem1.java create mode 100644 problem2.java create mode 100644 problem3.java diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..5c390329 --- /dev/null +++ b/problem1.java @@ -0,0 +1,35 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No, just needed to understand prefix sum logic. + + +// Your code here along with comments explaining your approach in three sentences only +// We use prefix sum and a hashmap to store how many times each prefix sum has appeared. +// At each index, we check if (currentSum - k) exists in the map and add its count to answer. +// Then we store the currentSum into the map and continue. + +import java.util.*; + +class Solution { + public int subarraySum(int[] nums, int k) { + + HashMap map = new HashMap<>(); + map.put(0, 1); + + int sum = 0; + int count = 0; + + for (int n : nums) { + sum += n; + + if (map.containsKey(sum - k)) { + count += map.get(sum - k); + } + + map.put(sum, map.getOrDefault(sum, 0) + 1); + } + + return count; + } +} diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..1ba1fd19 --- /dev/null +++ b/problem2.java @@ -0,0 +1,37 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No, just had to convert 0 to -1. + + +// Your code here along with comments explaining your approach in three sentences only +// We treat 0 as -1 and 1 as +1 and keep a running sum. +// If the same sum appears again, the subarray between them has equal number of 0s and 1s. +// We store the first index of each sum in a hashmap to get the maximum length. + +import java.util.*; + +class Solution { + public int findMaxLength(int[] nums) { + + HashMap map = new HashMap<>(); + map.put(0, -1); + + int sum = 0; + int maxLen = 0; + + for (int i = 0; i < nums.length; i++) { + + if (nums[i] == 0) sum -= 1; + else sum += 1; + + if (map.containsKey(sum)) { + maxLen = Math.max(maxLen, i - map.get(sum)); + } else { + map.put(sum, i); + } + } + + return maxLen; + } +} diff --git a/problem3.java b/problem3.java new file mode 100644 index 00000000..7f3281ae --- /dev/null +++ b/problem3.java @@ -0,0 +1,33 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) (since alphabet size is fixed) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No, just had to count odd frequency characters. + + +// Your code here along with comments explaining your approach in three sentences only +// We count frequency of each character using a hash set. +// If a character appears even times, it can fully be used in palindrome, and if odd, one can be placed in center. +// Finally, we add all usable pairs and add one odd if any exists. + +import java.util.*; + +class Solution { + public int longestPalindrome(String s) { + + HashSet set = new HashSet<>(); + int count = 0; + + for (char c : s.toCharArray()) { + if (set.contains(c)) { + set.remove(c); + count += 2; + } else { + set.add(c); + } + } + + if (!set.isEmpty()) count += 1; + + return count; + } +}