Day 31 of LeetCode

Day 31 of LeetCode

ยท

3 min read

Documenting LeetCode solving.

Q91

647. Palindromic Substrings

Medium.

Very similar to 5. Longest Palindromic Substring

class Solution {
    public int countSubstrings(String s) {
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            // odd
            res += countPali(s, i, i);
            // even
            res += countPali(s, i, i + 1);
        }
        return res;
    }

    public int countPali(String s, int l, int r) {
        int res = 0;
        while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
            res++;
            l--;
            r++;
        }
        return res;
    }
}

Q92

91. Decode Ways

Medium.

class Solution {

    public int numDecodings(String s) {
        int n = s.length();
        if (n < 1) return 0;

        // dp[i] -> number of decode ways s[0...i-1]
        int[] dp = new int[n + 1];

        // base case: s is empty
        dp[0] = 1;
        // base case: only one character
        dp[1] = s.charAt(0) == '0' ? 0 : 1;

        // Note: the index between the dp[] and s is offset by one
        for (int i = 2; i <= n; i++) {
            char one = s.charAt(i - 1);
            char two = s.charAt(i - 2);
            // s[i] as a letter
            if (one >= '1' && one <= '9') {
                dp[i] += dp[i - 1];
            }
            // 10 - 26
            if (two == '1' || (two == '2' && (one >= '0' && one <= '6'))) {
                dp[i] += dp[i - 2];
            }
        }

        return dp[n];
    }
}

R9

125. Valid Palindrome

Easy.

class Solution {
    public boolean isPalindrome(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char curr = s.charAt(i);
            if (Character.isLetterOrDigit(curr)) {
                sb.append(Character.toLowerCase(curr));
            }
        }

        s = sb.toString();
        int l = 0, r = s.length() - 1;
        while (l < r) {
            if (s.charAt(l) != s.charAt(r)) {
                return false;
            }
            l++;
            r--;
        }
        return true;
    }
}

R10

167. Two Sum II - Input Array Is Sorted

Medium.

class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int l = 0, r = numbers.length - 1;

        while (l < r) {
            int currSum = numbers[l] + numbers[r];
            if (currSum < target) {
                l++;
            }
            else if (currSum > target) {
                r--;
            }
            else {
                break;
            }
        }

        return new int[]{l + 1, r + 1};
    }
}

R11

15. 3Sum

Medium.

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();

        for (int i = 0; i < nums.length; i++) {
            int a = nums[i];
            // skip positive
            if (a > 0) break;
            // skip duplicate
            if (i > 0 && a == nums[i - 1]) continue;

            int l = i + 1, r = nums.length - 1;
            while (l < r) {
                int currSum = a + nums[l] + nums[r];
                if (currSum < 0) {
                    l++;
                }
                else if (currSum > 0) {
                    r--;
                }
                else {
                    res.add(new ArrayList<>(Arrays.asList(a, nums[l], nums[r])));
                    l++;
                    r--;
                    // skip duplicate nums[l]
                    while (l < r && nums[l] == nums[l - 1]) {
                        l++;
                    }
                }
            }
        }
        return res;  
    }
}

R12

11. Container With Most Water

Medium.

class Solution {
    public int maxArea(int[] height) {
        int l = 0, r = height.length - 1;
        int res = 0;

        while (l < r) {
            res = Math.max(res, Math.min(height[l], height[r]) * (r - l));
            if (height[l] < height[r]) {
                l++;
            }
            else {
                r--;
            }
        }

        return res;
    }
}

R13

42. Trapping Rain Water

Hard.

class Solution {
    public int trap(int[] height) {
        int l = 0, r = height.length - 1;
        int res = 0;
        int leftMax = height[l], rightMax = height[r];

        while (l < r) {
            if (height[l] < height[r]) {
                l++;
                leftMax = Math.max(leftMax, height[l]);
                res += leftMax - height[l];
            }
            else {
                r--;
                rightMax = Math.max(rightMax, height[r]);
                res += rightMax - height[r];
            }
        }

        return res;
    }
}

Did you find this article valuable?

Support ๐Ÿฐ Evelyn's Learning Journey by becoming a sponsor. Any amount is appreciated!

ย