# 241
class Solution:
def diffWaysToCompute(self, s : str) -> List[int]:
if s.isdigit(): return [int(s)]
res = []
for i, c in enumerate(s):
if c in '+-*':
for left in self.diffWaysToCompute(s[:i]):
for right in self.diffWaysToCompute(s[i+1:]):
res.append(eval(f'{left}{c}{right}'))
return res
# 242
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
tmp1 = Counter(s)
tmp2 = Counter(t)
if tmp1 == tmp2:
return True
return False
# 243
class Solution:
def shortestDistance(self, words: List[str], word1: str, word2: str) -> int:
res = len(words) - 1
pos1, pos2 = -1, -1
for i, word in enumerate(words):
if word == word1:
pos1 = i
elif word == word2:
pos2 = i
if pos1 != -1 and pos2 != -1:
res = min(res, abs(pos1 - pos2))
return res
# 244
class WordDistance:
def __init__(self, words: List[str]):
d = defaultdict(list)
for idx, value in enumerate(words):
d[value].append(idx)
self.words = d
self.rawwords = words
def shortest(self, word1: str, word2: str) -> int:
min_distance = len(self.rawwords)
idx1 = self.words[word1]
idx2 = self.words[word2]
for i1 in idx1:
for i2 in idx2:
min_distance = min(min_distance, abs(i1 - i2))
return min_distance
# 245
class Solution:
def shortestWordDistance(self, words: List[str], word1: str, word2: str) -> int:
res = len(words)
if word1 != word2:
pos1, pos2 = -1, -1
for i, word in enumerate(words):
if word == word1:
pos1 = i
elif word == word2:
pos2 = i
if pos2 != -1 and pos1 != -1:
res = min(res, abs(pos1 - pos2))
else:
pos = -res
for x,y in enumerate(words):
if word1 == y:
res = min(res , abs(pos - x))
pos = x
return res
# 246
class Solution:
def isStrobogrammatic(self, num: str) -> bool:
if not num:
return True
n = len(num)
tmp1 = ["2" , "3" , "4" , "5", "7"]
tmp2 = {"6" : "9" , "9" : "6" , "8" : "8" , "1" : "1" , "0" : "0"}
for i in range(n//2 + 1):
if num[i] in tmp1:
return False
if tmp2[num[i]] != num[n - i - 1]:
return False
return True
# 247
class Solution:
def findStrobogrammatic(self, n: int) -> List[str]:
dict = {'1':'1','6':'9','9':'6','8':'8'}
def fun(x):
if x == 0:
return ['']
if x == 1:
return ['1','8','0']
ans = []
for i in fun(x-2):
if x != n:
ans.append('0' + i + '0')
for y,z in dict.items():
ans.append(z + i + y)
return ans
return fun(n)
# 248
class Solution:
def func(self, n: int) -> List[str]:
self.pairs = [['1', '1'], ['8', '8'], ['6', '9'], ['9', '6']]
def dfs(x: int) -> List[str]:
if x == 0:
return [""]
if x == 1:
return ["0", "1", "8"]
ans = []
for s in dfs(x - 2):
for a, b in self.pairs:
ans.append(a + s + b)
if x != n:
ans.append('0' + s + '0')
return ans
return dfs(n)
def strobogrammaticInRange(self, low: str, high: str) -> int:
n1, n2 = len(low), len(high)
min_len = min(n1, n2)
max_len = max(n1, n2)
res = []
for n in range(min_len, max_len + 1):
for s in self.func(n):
if int(low) <= int(s) <= int(high):
res.append(s)
return len(res)
# 249
class Solution:
def groupStrings(self, strings: List[str]) -> List[List[str]]:
mode_list = defaultdict(list)
for s in strings:
if s[0] == 'a':
mode_list[s].append(s)
else:
tmp = list(s) #转化成一种模式 从'a****'
for i in range(len(s)):
tmp[i] = chr( (ord(tmp[i]) - ord(s[0]) + 26) % 26 + ord('a') )
tmp = ''.join(tmp)
mode_list[tmp].append(s)
res = []
for mode, sublist in mode_list.items():
res.append(sublist)
return res
# 250
class Solution:
def countUnivalSubtrees(self, root: TreeNode) -> int:
if not root:
return 0
self.count = 0
def dfs(root):
if not root:
return True
left, right = dfs(root.left), dfs(root.right)
if root.left and root.val != root.left.val:
return False
if root.right and root.val != root.right.val:
return False
if left and right:
self.count += 1
return True
return False
dfs(root)
return self.count