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
47 changes: 47 additions & 0 deletions ContiguousArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Time Complexity : O(N), Go through all the elements to find the max length
// Space Complexity : O(N), keep the sums for all the N values in the map in the worst case
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

// Approach :
// We will perform a zero-sum Search. By treating 0 as -1, any subarray where the 1s and -1s cancel each other out
// (summing to 0) must have an equal count of both.

//We are going to store Prefix Sum with a Hashmap to find these "balanced" sections. It tracks the running total as
// it loops through the array; if it hits a sum it has seen before, it knows the elements between those two points
// perfectly balanced out, and it calculates that distance to find the maximum length.
import java.util.HashMap;
import java.util.Map;

public class ContiguousArray {
public int findMaxLength(int[] nums) {
int len = nums.length;
if(len == 1) return 0;
// Keep the sum seen previously and the corresponding index, index will be help to find the max length
Map<Integer, Integer> previousSumIndexMap = new HashMap<>();

// Put the initial sum of 0 and index as -1, will be useful for calculating length at index 0
previousSumIndexMap.put(0, -1);

int currentSum = 0;
int maxLength = 0;
for(int index= 0; index < len; index++){
// Change 0 to -1, so that 1 and -1 balances each other out and total sum becomes 0
if(nums[index] == 0) {
nums[index] = -1;
}

currentSum += nums[index];
// check if the current sum as already appeared before, it indicates we found subarray with longer length
// calculate the new max length
if(previousSumIndexMap.containsKey(currentSum)){
int previousIndex = previousSumIndexMap.get(currentSum);
maxLength = Math.max(maxLength, index - previousIndex);
}else{
previousSumIndexMap.put(currentSum, index);
}
}

return maxLength;
}
}
49 changes: 49 additions & 0 deletions LongestPalindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Time Complexity : O(N), Go through all the elements to find the max length
// Space Complexity : O(N), keep all the sums in the map in the worst case
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

// Approach :
// A palindrome can have even number of characters or odd number of characters. Palindrome with total Odd number of
// characters is longer. To form the longest palindrome: we take everything from even counts of characters.
// And take the "even part" of odd counts: If a letter appears 5 times, you can still use 4 of them (2 on each side).
// And if there are odd number of characters in the string, add one to the result.
// Longest palindrome => even count of chars + even count out of odd number of chars + 1 (if it has odd numbers of characters)
import java.util.HashMap;
import java.util.Map;

public class LongestPalindrome {
public int longestPalindrome(String s) {
int length = s.length();
if (length == 1) return 1;

// Maintain a frequency map to maintain the characters and their count.
Map<Character, Integer> frequencyMap = new HashMap<>();
int result = 0;
for (int i = 0; i < length; i++) {
Character curr = s.charAt(i);
if (frequencyMap.containsKey(curr)) {
frequencyMap.put(curr, frequencyMap.get(curr) + 1);
} else {
frequencyMap.put(curr, 1);
}
}

boolean hasOddChars = false;
for (Integer count : frequencyMap.values()) {
// if the count is even add to the max length as it is.
if (count % 2 == 0) {
result += count;
} else {
// if the count is odd, add to the max even value from it and set flag has odd characters to true
result += count-1;
hasOddChars = true;
}
}

if(hasOddChars){
result += 1;
}
return result;
}
}
46 changes: 46 additions & 0 deletions SubArraySum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Time Complexity : O(N), will go through each element
// Space Complexity : O(N), we need to keep the previous sum for all indices
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

// Approach :
// The brute force way is to run 2 for loops and calculate the sum between index i and j and check if it is equal to k.
// To avoid using the inner loop, we need to store the previous sum (till index i) in the map. So that at any index we
// can check if the total sum (at index i) - previous sum(i-1) is equal to K.
// At any point, we will know the total sum and K, then we can check (total sum - k) = previous sum is in the map or not.
// In other words, if the total sum is current sum of the sub array, do we have any subarray value in the map that is total sum - k;
// cs - ps = k, thus
// cs - k = ps and ps is stored in the map.

import java.util.HashMap;
import java.util.Map;

public class SubArraySum {

public int subarraySum(int[] nums, int k) {
int len = nums.length;
if(len == 0) return 0;
if(len == 1) return nums[0] == k ? 1 : 0;


// Use hashmap to keep the previous sum and the count of how many times it has appeared before. It indicates that
// the current sum can form multiple subarray with value K, so this count should be added in the result.
Map<Integer, Integer> previousSumMap = new HashMap<>();
// Initialize the map indicating the previous sum is 0 and has occurred once. It is useful
// for checking the previous sum at index 0;
previousSumMap.put(0, 1);

int count = 0;
int currentSum = 0;
for (int num : nums) {
currentSum += num;
if (previousSumMap.containsKey(currentSum - k)) {
count += previousSumMap.get(currentSum - k);
}
// store the current sum with updated count
previousSumMap.put(currentSum, previousSumMap.getOrDefault(currentSum, 0) + 1);
}

return count;
}
}