Leetcode Coding Journey: Mastering Problem-Solving Techniques

ยท

1 min read

Leetcode #10 Regular Expression Matching

๐Ÿ“Œ Problem Statement

Given an input string s and a pattern p, implement regular expression matching with support for:
- . Matches any single character.
- Matches *zero or more of the preceding element**.
- The matching should cover the entire input string (not partial).

๐Ÿš€ Code Implementation

class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        set1={}
        def match(i,j):
            if (i,j) in set1:
                return set1[(i,j)]        
            if j==len(p):
                return i==len(s)
            match1=i<len(s) and (s[i]==p[j] or p[j]=='.')
            if j+1<len(p) and p[j+1]=='*':
                res=match(i,j+2) or (match1 and match(i+1,j))
            else:
                res=match1 and match(i+1,j+1)
            set1[(i,j)]=res
            return res
        return match(0,0)

Leetcode #11 Container With Most Water

๐Ÿ“Œ Problem Statement

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i-th line are (i, 0) and (i, height[i]).

Find two lines that, together with the x-axis, form a container that can store the most water.

Return the maximum amount of water a container can store.

๐Ÿš€ Code Implementation

class Solution(object):
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        left=0
        right=len(height) - 1
        max_area=0
        while left<right:
            area=(right-left)*min(height[left],height[right])
            max_area=max(max_area,area)
            if height[left]<height[right]:
                left+=1
            else:
                right-=1
        return max_area
ย