0% found this document useful (0 votes)
11 views2 pages

Week 3 Questions and Answers

The document contains questions and answers related to C and C++ programming. It includes solutions to two Leetcode problems: 'Interleaving String' and 'Validate Binary Search Tree', with Python code provided for each. Additionally, it offers links to the respective problem pages on Leetcode.

Uploaded by

Harish .i
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Week 3 Questions and Answers

The document contains questions and answers related to C and C++ programming. It includes solutions to two Leetcode problems: 'Interleaving String' and 'Validate Binary Search Tree', with Python code provided for each. Additionally, it offers links to the respective problem pages on Leetcode.

Uploaded by

Harish .i
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Week 3 Questions and Answers

C Programming

Provide relevant answers here if there are questions about C programming.

C++ Programming

Provide relevant answers here if there are questions about C++ programming.

Leetcode Questions

1. Interleaving String

Problem Link: https://leetcode.com/problems/interleaving-string

Solution (Python):

def isInterleave(s1, s2, s3):

if len(s1) + len(s2) != len(s3):

return False

dp = [[False] * (len(s2) + 1) for _ in range(len(s1) + 1)]

dp[0][0] = True

for i in range(1, len(s1) + 1):

dp[i][0] = dp[i - 1][0] and s1[i - 1] == s3[i - 1]

for j in range(1, len(s2) + 1):

dp[0][j] = dp[0][j - 1] and s2[j - 1] == s3[j - 1]

for i in range(1, len(s1) + 1):

for j in range(1, len(s2) + 1):

dp[i][j] = (dp[i - 1][j] and s1[i - 1] == s3[i + j - 1]) or \

(dp[i][j - 1] and s2[j - 1] == s3[i + j - 1])


return dp[len(s1)][len(s2)]

2. Validate Binary Search Tree

Problem Link: https://leetcode.com/problems/validate-binary-search-tree

Solution (Python):

class Solution:

def isValidBST(self, root, low=float('-inf'), high=float('inf')):

if not root:

return True

if not (low < root.val < high):

return False

return (self.isValidBST(root.left, low, root.val) and

self.isValidBST(root.right, root.val, high))

You might also like