classSolution:deffindMedianSortedArrays(self,nums1,nums2):n1,n2=len(nums1),len(nums2)# Ensure nums1 is the smaller arrayifn1>n2:nums1,nums2,n1,n2=nums2,nums1,n2,n1total_len=n1+n2half_len=(total_len+1)//2low,high=0,n1whilelow<=high:mid1=(low+high)//2mid2=half_len-mid1# Handling out-of-bound indicesl1=nums1[mid1-1]ifmid1>0elsefloat('-inf')r1=nums1[mid1]ifmid1<n1elsefloat('inf')l2=nums2[mid2-1]ifmid2>0elsefloat('-inf')r2=nums2[mid2]ifmid2<n2elsefloat('inf')# Check if partition is correctifl1<=r2andl2<=r1:# Found the medianiftotal_len%2==1:returnmax(l1,l2)# Odd length caseelse:return(max(l1,l2)+min(r1,r2))/2.0# Even length caseelifl1>r2:high=mid1-1# Move left in nums1else:low=mid1+1# Move right in nums1# In case input arrays are not sorted, which theoretically shouldn't happen for valid inputsraiseValueError("Input arrays are not sorted")
classSolution:deflongestPalindrome(self,s):ifnots:return""start,max_len=0,1defexpand_around_center(left,right):whileleft>=0andright<len(s)ands[left]==s[right]:left-=1right+=1returnleft+1,right-1foriinrange(len(s)):# Case 1: Odd-length palindromeleft1,right1=expand_around_center(i,i)ifright1-left1+1>max_len:start,max_len=left1,right1-left1+1# Case 2: Even-length palindromeleft2,right2=expand_around_center(i,i+1)ifright2-left2+1>max_len:start,max_len=left2,right2-left2+1returns[start:start+max_len]