Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/BinarySubArray.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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;
}

}
31 changes: 31 additions & 0 deletions src/LongestPalindrome.java
Original file line number Diff line number Diff line change
@@ -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<Character> 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;
}
}
30 changes: 30 additions & 0 deletions src/SubArraySumK.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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;
}
}
24 changes: 24 additions & 0 deletions src/TestHashing2.java
Original file line number Diff line number Diff line change
@@ -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));
}
}