classSolution: defmaximumNumberOfStringPairs(self, words: List[str]) -> int: res = 0 for i inrange(len(words)): for j inrange(i+1, len(words)): if words[i] == words[j][::-1]: res += 1
# 从x开始 x, y, z = xx, yy, zz prev_res = "" status = "" if x > 0: now_res = "AA"# 从x开始 status = "x" x -= 1 while prev_res != now_res: prev_res = now_res if status == "x": if y > 0: y -= 1 now_res += "BB" status = "y" else: break elif status == "y": flag = False if z > 0: z -= 1 now_res += "AB" status = "z" flag = True elifnot flag and x > 0: x -= 1 now_res += "AA" status = "x" else: break elif status == "z": flag = False if z > 0: z -= 1 now_res += "AB" status = "z" elifnot flag and x > 0: x -= 1 now_res += "AA" status = "x" else: break print("x: now_res", now_res) res = max(res, len(now_res))
# 从y开始 x, y, z = xx, yy, zz prev_res = "" status = "" if y > 0: now_res = "BB"# 从y开始 status = "y" y -= 1 while prev_res != now_res: prev_res = now_res if status == "x": if y > 0: y -= 1 now_res += "BB" status = "y" else: break elif status == "y": flag = False if z > 0: z -= 1 now_res += "AB" status = "z" flag = True elifnot flag and x > 0: x -= 1 now_res += "AA" status = "x" else: break elif status == "z": flag = False if z > 0: z -= 1 now_res += "AB" status = "z" elifnot flag and x > 0: x -= 1 now_res += "AA" status = "x" else: break print("y: now_res", now_res) res = max(res, len(now_res))
# 从z开始 x, y, z = xx, yy, zz prev_res = "" status = "" if z > 0: now_res = "AB"# 从z开始 status = "z" z -= 1 while prev_res != now_res: prev_res = now_res if status == "x": if y > 0: y -= 1 now_res += "BB" status = "y" else: break elif status == "y": flag = False if z > 0: z -= 1 now_res += "AB" status = "z" flag = True elifnot flag and x > 0: x -= 1 now_res += "AA" status = "x" else: break elif status == "z": flag = False if z > 0: z -= 1 now_res += "AB" status = "z" elifnot flag and x > 0: x -= 1 now_res += "AA" status = "x" else: break print("z: now_res", now_res) res = max(res, len(now_res))