diff --git a/BruteForce.java b/BruteForce.java new file mode 100644 index 00000000..d9f8ae34 --- /dev/null +++ b/BruteForce.java @@ -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; + } + +} \ No newline at end of file diff --git a/ContiguousArray.java b/ContiguousArray.java new file mode 100644 index 00000000..ae78d148 --- /dev/null +++ b/ContiguousArray.java @@ -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 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; + } +} \ No newline at end of file diff --git a/LongestPalindrome.java b/LongestPalindrome.java new file mode 100644 index 00000000..842bac18 --- /dev/null +++ b/LongestPalindrome.java @@ -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 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; + + } +} \ No newline at end of file diff --git a/Sample.java b/Sample.java index 1739a9cb..1a63df0a 100644 --- a/Sample.java +++ b/Sample.java @@ -5,3 +5,69 @@ // Your code here along with comments explaining your approach + +class Sample { + // 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 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; + } + + + // 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 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; + } + + // 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 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; + + } +} diff --git a/SubarraySum.java b/SubarraySum.java new file mode 100644 index 00000000..8ca8facd --- /dev/null +++ b/SubarraySum.java @@ -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 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; + } +} \ No newline at end of file