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
35 changes: 35 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Time Complexity : O(n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No, just needed to understand prefix sum logic.


// Your code here along with comments explaining your approach in three sentences only
// We use prefix sum and a hashmap to store how many times each prefix sum has appeared.
// At each index, we check if (currentSum - k) exists in the map and add its count to answer.
// Then we store the currentSum into the map and continue.

import java.util.*;

class Solution {
public int subarraySum(int[] nums, int k) {

HashMap<Integer, Integer> map = new HashMap<>();
map.put(0, 1);

int sum = 0;
int count = 0;

for (int n : nums) {
sum += n;

if (map.containsKey(sum - k)) {
count += map.get(sum - k);
}

map.put(sum, map.getOrDefault(sum, 0) + 1);
}

return count;
}
}
37 changes: 37 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Time Complexity : O(n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No, just had to convert 0 to -1.


// Your code here along with comments explaining your approach in three sentences only
// We treat 0 as -1 and 1 as +1 and keep a running sum.
// If the same sum appears again, the subarray between them has equal number of 0s and 1s.
// We store the first index of each sum in a hashmap to get the maximum length.

import java.util.*;

class Solution {
public int findMaxLength(int[] nums) {

HashMap<Integer, Integer> map = new HashMap<>();
map.put(0, -1);

int sum = 0;
int maxLen = 0;

for (int i = 0; i < nums.length; i++) {

if (nums[i] == 0) sum -= 1;
else sum += 1;

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

return maxLen;
}
}
33 changes: 33 additions & 0 deletions problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Time Complexity : O(n)
// Space Complexity : O(1) (since alphabet size is fixed)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No, just had to count odd frequency characters.


// Your code here along with comments explaining your approach in three sentences only
// We count frequency of each character using a hash set.
// If a character appears even times, it can fully be used in palindrome, and if odd, one can be placed in center.
// Finally, we add all usable pairs and add one odd if any exists.

import java.util.*;

class Solution {
public int longestPalindrome(String s) {

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

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

if (!set.isEmpty()) count += 1;

return count;
}
}