1122. Relative Sort Array

class Solution: def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]: dict_sort={} cnt=1 for num in arr2: dict_sort[num]=cnt cnt+=1 for num in arr1: if num not in dict_sort: dict_sort[num]=cnt+1 arr1.sort() arr1.sort(key=lambda x: dict_sort[x]) return arr1

1128. Number of Equivalent Domino Pairs

class Solution: def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int: rep=0 tmp=[0 for i in dominoes] for i in range(len(dominoes)-1): if tmp[i]!=0: continue cnt=0 for j in range(i+1,len(dominoes)): if tmp[j]==0 and ((dominoes[i][0]==dominoes[j][0] and dominoes[i][1]==dominoes[j][1]) or (dominoes[i][0]==dominoes[j][1] and dominoes[i][1]==dominoes[j][0])): tmp[j]=1 cnt+=1 rep+=(cnt+1)*cnt//2 return rep

1155. Number of Dice Rolls With Target Sum

class Solution: def numRollsToTarget(self, d: int, f: int, target: int) -> int: dict_f={} for i in range(1,f+1): dict_f[i]=1 while d>1: dict_s={} for i in range(1,f+1): for key in dict_f: if i+key not in dict_s: dict_s[i+key]=dict_f[key] else: dict_s[i+key]+=dict_f[key] dict_f=dict_s d-=1 if target in dict_f: return dict_f[target]%(10**9 + 7) else: return 0

1156. Swap For Longest Repeated Character Substring

class Solution: def maxRepOpt1(self, text: str) -> int: rep=1 for k in range(1,len(text)-1): if text[k]!=text[k-1]: i=0 cnt=1 target=text[k-1] left=k-1-1 right=k+1 while left>=0 and text[left]==target: left-=1 cnt+=1 while right<len(text) and text[right]==target: right+=1 cnt+=1 if left>0: if target in text[:left] or target in text[right:]: rep=max(rep,cnt+1) else: rep=max(rep,cnt) else: if target in text[right:]: rep=max(rep,cnt+1) else: rep=max(rep,cnt) target=text[0] cnt=1ContinueContinue reading “1156. Swap For Longest Repeated Character Substring”

1160. Find Words That Can Be Formed by Characters

class Solution: def countCharacters(self, words: List[str], chars: str) -> int: dict_chars=self.stringToDict(chars) rep=0 for word in words: dict_word=self.stringToDict(word) if self.charsContainsWord(dict_chars,dict_word): rep+=len(word) return rep def charsContainsWord(self,dict_chars,dict_word): for k in dict_word: if k not in dict_chars: return False elif dict_chars[k]<dict_word[k]: return False return True def stringToDict(self,word): dict_word={} for c in word: if c not in dict_word: dict_word[c]=1 else:ContinueContinue reading “1160. Find Words That Can Be Formed by Characters”

1170. Compare Strings by Frequency of the Smallest Character

class Solution: def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]: queries_int=[] words_int=[] for query in queries: queries_int.append(self.wordToInt(query)) for word in words: words_int.append(self.wordToInt(word)) words_int.sort() rep=[] for num in queries_int: rep.append(self.getNumberLagerN(num,words_int[::-1])) return rep def getNumberLagerN(self,num,words_int): cnt=0 for n in words_int: if n>num: cnt+=1 else: break return cnt def wordToInt(self,word): strs=”” for s in “abcdefghijklmnopqrstuvwxyz”: if s inContinueContinue reading “1170. Compare Strings by Frequency of the Smallest Character”

1184. Distance Between Bus Stops

class Solution: def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int: if destination<start: destination,start=start,destination num1_0ToSta=self.getdistance(0,start,distance) num2_staToDes=self.getdistance(start,destination,distance) num3_desTo0=self.getdistance(destination,len(distance),distance) return min(num2_staToDes,num1_0ToSta+num3_desTo0) def getdistance(self,k1,k2,distance): rep=0 for k in range(k1,k2): rep+=distance[k] return rep

1189. Maximum Number of Balloons

”’ Given a string text, you want to use the characters of text to form as many instances of the word “balloon” as possible. You can use each character in text at most once. Return the maximum number of instances that can be formed. Example 1: Input: text = “nlaebolko” Output: 1 Example 2: Input:ContinueContinue reading “1189. Maximum Number of Balloons”