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
24 changes: 24 additions & 0 deletions BruteForce.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class BruteForce{
public int subarraySum(int[] nums, int k) {
int [] nums2 = new int[nums.length];
int ans = 0;
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum = sum + nums[i];
nums2[i] = sum;
}

for (int i = 0; i < nums2.length; i++) {
if (nums2[i] == k) {
ans ++;
}
for (int j = i + 1; j < nums2.length; j ++) {
if ( nums2[j] - nums2[i] == k ) {
ans ++;
}
}
}
return ans;
}

}
23 changes: 23 additions & 0 deletions ContiguousArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.HashMap;
class ContiguousArray {

// time complexity O(n)
// space complexity O(n)
// ran successfully in leetcode
// the logic keeps track of the sum incrementing if 1 else decrementing
public int findMaxLength(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
int maxLength = 0, sum = 0;
map.put(0 , -1); // base case for sum 0

for (int i = 0; i < nums.length; i++) {
sum = (nums[i] == 1) ? sum + 1: sum - 1;
if(map.containsKey(sum)) {
maxLength = (i - map.get(sum) > maxLength) ? i - map.get(sum): maxLength;
} else {
map.put(sum, i);
}
}
return maxLength;
}
}
23 changes: 23 additions & 0 deletions LongestPalindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.HashSet;

class LongestPalindrome {

// time complexity O(n)
// space complexity O(1) since there are only 26 characters
// ran successfully in leetcode
// the logic keeps adds in the set if character is not else removes it and adds 2 to the maxlength
public int longestPalindrome(String s) {
HashSet<Character> set = new HashSet<>();
int len = 0;
for (char ch : s.toCharArray()) {
if (set.contains(ch)) {
set.remove(ch);
len += 2;
} else {
set.add(ch);
}
}
return set.size() > 0 ? len + 1 : len;

}
}
25 changes: 25 additions & 0 deletions SubarraySum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class SubarraySum {
// time complexity O(n)
// space complexity O(n)
// ran successfully in leetcode
// the logic keeps track of the prevous count and sum of the subarray
public int subarraySum(int[] nums, int k) {
Map<Integer, Integer> previousCount = new HashMap<>();
previousCount.put(0, 1); // base case, if a sum of 0 is required in the future count increments by one

int sum = 0;
int count = 0;

for (int num : nums) {
sum += num;
// if the sum difference is already there then calculate the
if (previousCount.containsKey(sum - k)) {
count += previousCount.get(sum - k);
}
// update the count
previousCount.put(sum, previousCount.getOrDefault(sum, 0) + 1);
}

return count;
}
}