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
20 changes: 20 additions & 0 deletions ContiguousArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class ContiguousArray {
public int findMaxLength(int[] nums) {

HashMap<Integer, Integer> rSum = new HashMap<>();
rSum.put(0, -1);
int sum = 0, max = 0;
for(int i = 0; i < nums.length; i++) {
sum += nums[i] == 0 ? -1 : 1;

if(rSum.containsKey(sum)) {
max = Math.max(max, i - rSum.get(sum));
} else {
rSum.put(sum, i);
}
}

return max;

}
}
18 changes: 18 additions & 0 deletions LongestPalindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class LongestPalindrome {
public int longestPalindrome(String s) {

Set<Character> dict = new HashSet<>();
int count = 0;

for(char c : s.toCharArray()) {
if(dict.contains(c)) {
count += 2;
dict.remove(c);
} else {
dict.add(c);
}
}

return dict.size() > 0 ? count + 1 : count;
}
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
Explain your approach in **three sentences only** at top of your code


## Problem1 (https://leetcode.com/problems/subarray-sum-equals-k/)
## Problem1 SubArraySum (https://leetcode.com/problems/subarray-sum-equals-k/)


## Problem2 (https://leetcode.com/problems/contiguous-array/)
## Problem2 ContiguousArray (https://leetcode.com/problems/contiguous-array/)


## Problem3 (https://leetcode.com/problems/longest-palindrome)
## Problem3 LongestPalindrome (https://leetcode.com/problems/longest-palindrome)
21 changes: 21 additions & 0 deletions SubArraySum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Using RSum to calculate the sum at that Index
// Add a default entry in the map with [0, 1] to make sure if the comp(rSum -k) is 0 then the array until that elemnt will be 1 sub array
// if different between running sum to k is available in the map then a sub array is found
// insert element in the map based on running sum and counter
class SubArraySum {
public int subarraySum(int[] nums, int k) {
HashMap<Integer, Integer> rSumCounterMap = new HashMap<>();
int rSum = 0;
rSumCounterMap.put(0, 1);
int count = 0;
for(int i = 0; i< nums.length;i++) {
rSum += nums[i];

if(rSumCounterMap.containsKey(rSum - k)) {
count += rSumCounterMap.get(rSum - k);
}
rSumCounterMap.merge(rSum, 1, Integer::sum);
}
return count;
}
}