Add files via upload
Swadhin Biswas committed 2 years ago
aca18cf
1 parent 264a159
commit aca18cf
2 files changed
+45 -0
Showing 2 changed files+45-0
▼Longest Palindromic Substring.py
+29-0
| @@ -0,0 +1,29 @@ | ||
1 | +class Solution: | |
2 | + def longestPalindrome(self, s: str) -> str: | |
3 | + if s[::-1] == s: | |
4 | + return s | |
5 | + | |
6 | + # inti start at 0 and window size is 1 | |
7 | + start, size = 0, 1 | |
8 | + | |
9 | + for i in range(1, len(s)): | |
10 | + # set up l, r pointer , base on cur idx to left | |
11 | + # cur window size = max window size + 1 | |
12 | + l, r = i - size, i + 1 | |
13 | + | |
14 | + # cur window size = max window size + 2 | |
15 | + s1 = s[l-1:r] | |
16 | + | |
17 | + # if s1 is palindrome, update start idx and the size | |
18 | + if l >= 1 and s1[::-1] == s1: | |
19 | + size += 2 | |
20 | + start = l - 1 | |
21 | + else: | |
22 | + # cur window size = max window size + 1 | |
23 | + s2 = s[l:r] | |
24 | + if s2[::-1] == s2: | |
25 | + size += 1 | |
26 | + start = l | |
27 | + | |
28 | + return s[start:start+size] | |
29 | + | |
0 | 30 | |
▼Median of Two Sorted Arrays.py
+16-0
| @@ -0,0 +1,16 @@ | ||
1 | +from typing import List | |
2 | +class Solution: | |
3 | + def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: | |
4 | + nums1.extend(nums2) | |
5 | + nums1.sort() | |
6 | + if len(nums1)%2==0: | |
7 | + return (nums1[len(nums1)//2]+nums1[(len(nums1)//2)-1])/2 | |
8 | + else: | |
9 | + return nums1[len(nums1)//2] | |
10 | + | |
11 | + | |
12 | + | |
13 | + | |
14 | + | |
15 | +b=Solution() | |
16 | +print(b.findMedianSortedArrays([1,3],[2])) | |
0 | 17 | |