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
27 changes: 27 additions & 0 deletions ContiguousArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Time Complexity : O(n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : no


// Your code here along with comments explaining your approach
/*
I would maintain a map to store the running sum of the values and respective index. If at any time,
sum repeats(most likely 0 as 1-1 = 0),it means equal number of 1's and 0's are present till that index. So we try to obtain the difference of those indices to get the maxLength. As an edge case, we give 0 for -1 index for maxLength scenario where we see early running sum in short array.
*/
class Solution {
public int findMaxLength(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
map.put(0, -1);
int sum = 0;
int maxLength = 0;
for(int i = 0 ; i < nums.length ; i++) {
sum += (nums[i] == 1 ? 1 : -1);
if(map.containsKey(sum))
maxLength = Math.max(maxLength, i - map.get(sum));
else
map.put(sum, i);
}
return maxLength;
}
}
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.

27 changes: 27 additions & 0 deletions SubarraySumEqualsK.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Time Complexity : O(n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : no


// Your code here along with comments explaining your approach
/*
I would maintain a map to store the running sum of the values and respective count occurence. If at any time, the difference of the running sum and target value k which is also a sum appears in the map already, it means we have already made certain sub arrays which satisfy the condition, so we want to make sure we add those as well to our overall count. If running sum is not present in the map, then we simply add it to the map. As an edge case, we give 0 for 1 count for subarraysum scenario where we see early running sum as 0 in short array.
*/
class Solution {
public int subarraySum(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
int rSum = 0;
int count = 0;
map.put(0, 1);
for(int i = 0 ; i < nums.length ; i++) {
rSum += nums[i];
int cmp = rSum - k;
if(map.containsKey(cmp)) {
count += map.get(cmp);
}
map.put(rSum, map.getOrDefault(rSum , 0) + 1);
}
return count;
}
}
36 changes: 36 additions & 0 deletions longestPalindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Time Complexity : O(n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : no


// Your code here along with comments explaining your approach
/*
I would take a map to store the occurences of the characters in given string. Now, if we know there
are even occurences, they tend to reflect same in reverse as well. So I directly increment my count
variable with that value. If odd occurences, I would need only even portion of it for the output to
be same in reverse order. So, I increment my count with those occurences - 1. I also keep track of
odd occurence existence using a flag. While final return of count, check if flag reflects odd occurence presence, if so, return extra one to the count as it would not change the palindrome and
max value is asked.
*/

class Solution {
public int longestPalindrome(String s) {
Map<Character, Integer> map = new HashMap<>();
boolean flag = false;
int count = 0;
for(int i = 0 ; i < s.length() ; i++) {
char ch = s.charAt(i);
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
for(int i : map.values()) {
if(i%2 == 0)
count += i;
else {
count += i - 1;
flag = true;
}
}
return flag == true ? count + 1 : count;
}
}