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
49 changes: 49 additions & 0 deletions Leetcode409.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Solution {
public int longestPalindrome(String s) {
// HashSet approach
// TC= O(n) iterate over the string length
// SC = O(1) max of 52 characters can be present in the hashset.
HashSet<Character> set = new HashSet<>();
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (set.contains(c)) { // check if a character is already present in the set, if its add count+2 and
// remove the element.
count += 2;
set.remove(c);
} else { // if not, add the character to the set
set.add(c);
}
}
if (set.size() >= 1) { // if the set is not empty, it means there's another character that could be
// added into the palindrome.
count += 1;
}
return count;

// Solution 2: HashMap approach
// TC = O(n)
// SC = O(1) max of 52 characters can be present in the hashset.

// HashMap<Character, Integer> map = new HashMap<>();
// int count =0;
// for (int i=0; i<s.length(); i++) {
// char c = s.charAt(i);
// map.put(c, map.getOrDefault(c,0)+1);
// }
// boolean odd = false; // Variable to keep track if a odd counted character is
// added or not.
// for(Integer value : map.values()){
// if(!odd && value%2==1) { // if odd numbered character is not added yet
// count += value; // add the value
// odd =true;
// } else if(value%2==1) { // if an odd numbered charater is already added, then
// one less even number.
// count +=value-1;
// } else { // all even numbers can be added as is.
// count +=value;
// }
// }
// return count;
}
}
72 changes: 72 additions & 0 deletions Leetcode525.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//TC: O(n)
//SC: O(n)
class Solution {
public int findMaxLength(int[] nums) {
// HashMap Solution with prefix sum

// int[] prefixSum = new int[nums.length+1];
// for(int i=0; i< nums.length; i++) {
// if(nums[i]==0) {
// prefixSum[i+1] = prefixSum[i]-1;
// } else {
// prefixSum[i+1] = prefixSum[i]+1;
// }
// }

// HashMap<Integer, Integer> count = new HashMap<>();
// int max =0;

// for (int i =0; i<prefixSum.length;i++) {
// if(!count.containsKey(prefixSum[i])) {
// count.put(prefixSum[i], i);
// } else {
// max= Math.max(max, i-count.get(prefixSum[i]));
// }
// }
// return max;

// Solution 2: Single For Loop with Prefix array

// int[] prefixSum = new int[nums.length+1];
// HashMap<Integer, Integer> count = new HashMap<>();
// int max =0;
// count.put(0,0);
// for(int i=0; i< nums.length; i++) {
// if(nums[i]==0) {
// prefixSum[i+1] = prefixSum[i]-1;
// } else {
// prefixSum[i+1] = prefixSum[i]+1;
// }

// if(!count.containsKey(prefixSum[i+1])) {
// count.put(prefixSum[i+1], i+1);
// } else {
// max= Math.max(max, i+1-count.get(prefixSum[i+1]));
// }
// }
// return max;

// Solution 3 : without prefix Sum array
// Runtime seems to be faster than the earlier two due to not creating an array
// and accessing it.
HashMap<Integer, Integer> map = new HashMap<>();
int max = 0;
int prefixSum = 0;
map.put(0, -1);
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
prefixSum -= 1; // Keeps track of prefix sum at index i of nums array.
} else {
prefixSum += 1;
}

if (!map.containsKey(prefixSum)) {
map.put(prefixSum, i);
} else {
max = Math.max(max, i - map.get(prefixSum));
}
}
return max;

}
}
45 changes: 45 additions & 0 deletions Leetcode560.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//TC : O(n)
// SC : O(n)
class Solution {
public int subarraySum(int[] nums, int k) {

// // Hash Map + Prefix with Two loops
// int[] prefixSum = new int[nums.length+1];

// for(int i =0; i< nums.length; i++){
// prefixSum[i+1] = prefixSum[i] + nums[i];
// }
// int count =0;
// HashMap<Integer, Integer> map = new HashMap<>();

// for(int i=0; i<prefixSum.length; i++) {
// if(map.containsKey(prefixSum[i]-k)) { //0, 1, 0, 3
// int key = prefixSum[i] -k;
// count+= map.get(prefixSum[i]-k); // _, _, 1, 2
// }
// map.put(prefixSum[i], map.getOrDefault(prefixSum[i], 0)+1); // _ , _ , {...
// 0:2}
// }

// return count;

// Hash Map + Prefix with Single loop

int[] prefixSum = new int[nums.length + 1];
int count = 0;
HashMap<Integer, Integer> map = new HashMap<>();
map.put(0, 1);

for (int i = 0; i < nums.length; i++) {
prefixSum[i + 1] = prefixSum[i] + nums[i];
if (map.containsKey(prefixSum[i + 1] - k)) {
int key = prefixSum[i + 1] - k;
count += map.get(prefixSum[i + 1] - k);
}
map.put(prefixSum[i + 1], map.getOrDefault(prefixSum[i + 1], 0) + 1);

}
return count;

}
}