if isPalindrome(a) and isPalindrome(b): returnTrue n = len(a)
if n & 1: l = r = n >> 1 else: l, r = (n >> 1) - 1, n >> 1 # print(len(a)) # print(l, r) al, ar = l, r while al >= 0and ar < n and a[al] == a[ar]: al -= 1 ar += 1 bl, br = l, r while bl >= 0and br < n and b[bl] == b[br]: bl -= 1 br += 1 # print(a[:al + 1], b[ar:][::-1]) # print(b[:bl + 1], a[br:][::-1]) return a[:al + 1] == b[ar:][::-1] or b[:bl + 1] == a[br:][::-1] \ or b[:al + 1] == a[ar:][::-1] or a[:bl + 1] == b[br:][::-1]
defcountSubgraphsForEachDiameter(self, n, edges): G = [[] for _ in range(n)] for i, j in edges: G[i - 1].append(j - 1) G[j - 1].append(i - 1)
defmaxd(mask0): res = 0 for i in range(n): if (1 << i) & mask0: mask = mask0 bfs, bfs2 = [i], [] cur = 0 while bfs: for i in bfs: mask ^= 1 << i for j in G[i]: if mask & (1 << j): bfs2.append(j) cur += 1 bfs, bfs2 = bfs2, [] if mask: return0 res = max(res, cur - 1) return res
res = [0] * (n - 1) for mask in range(1 << n): if mask & (mask - 1) == 0: continue d = maxd(mask) if d: res[d - 1] += 1