# A. Remove Letter To Equalize Frequency (opens new window)
Try every character, check if the remaining string is equalized.
DETAILS
def equalFrequency(self, word: str) -> bool:
for i in range(len(word)):
if len(set(Counter(word[:i] + word[i + 1:]).values())) == 1:
return True
return False
# B. Longest Uploaded Prefix (opens new window)
Use a pointer to the end of the current longest prefix.
DETAILS
class LUPrefix:
def __init__(self, n: int):
self.list = [0] * n
self.p = -1
self.n = n
def upload(self, video: int) -> None:
self.list[video - 1] += 1
while self.p < self.n - 1 and self.list[self.p + 1] == 1:
self.p += 1
def longest(self) -> int:
return self.p + 1
# 2425. Bitwise XOR of All Pairings (opens new window)
XOR
is self-inverting, commutative and associative:
a ^ a = 0
a ^ b = b ^ a
a ^ b ^ c = a ^ (b ^ c)
Therefore, start with ans = 0
:
If
len(B)
is even: each number inA
appears even times, thus they are all offseted,ans ^= 0
If
len(B)
is odd, each number inA
appears odd times, thus all occurences are all offseted except the last one,ans ^= XOR(A)
If
len(A)
is even: each number inB
appears even times, thus they are all offseted,ans ^= 0
If
len(A)
is odd, each number inB
appears odd times, thus all occurences are all offseted except the last one,ans ^= XOR(B)
DETAILS
def xorAllNums(self, A: List[int], B: List[int]) -> int:
a, b = reduce(lambda a, b: a^b, A), reduce(lambda a, b: a^b, B)
m, n = len(A), len(B)
if m % 2 and n % 2: return a ^ b
elif m % 2: return b
elif n % 2: return a
return 0
# D. Number of Pairs Satisfying Inequality (opens new window)
Binary search and insert on sorted container.
DETAILS
from sortedcontainers import SortedList
class Solution:
def numberOfPairs(self, A: List[int], B: List[int], diff: int) -> int:
D, sl = [a - b for a, b in zip(A, B)], SortedList()
res, n = 0, len(A)
for d in D:
res += sl.bisect_right(d + diff)
sl.add(d)
return res