Given two lists of strings list1 and list2, determine whether they have the same checksum, as a way to verify that two copies of some data are consistent.
The checksum of a list is the sum of the rolling hashes of its strings. The rolling hash of a string s of length n is
hash(s)=ord(s0)⋅31n−1+ord(s1)⋅31n−2+⋯+ord(sn−1)⋅310
where ord(c) is the ASCII code of the character c. All arithmetic is performed modulo M=109+7.
Return True if the two checksums are equal and False otherwise.
Function Signature
def data_integrity_checker(list1: list[str], list2: list[str]) -> bool:
Rules
-
The hash of an empty string is 0.
-
The sum of the hashes is also taken modulo M, so each checksum is a value from 0 to M - 1.
-
The two lists may have different lengths. Only the two checksums are compared, so two lists with different contents can still have equal checksums.
Constraints
-
1 <= len(list1), len(list2) <= 10^4
-
0 <= len(list1[i]), len(list2[i]) <= 1000
-
All strings consist of lowercase English letters.
Examples
Input: list1 = ["hello","world"], list2 = ["hello","world"]
Output: True
Both lists hold the same strings in the same order, so their hashes and their checksums are identical.
Input: list1 = ["abc","def"], list2 = ["abc","deg"]
Output: False
"def" and "deg" differ only in the last character, whose multiplier is 1. The hash of "deg" is the hash of "def" plus 1 (99333 versus 99334), so the checksums differ.
Input: list1 = ["ab","ba"], list2 = ["aa","bb"]
Output: True
The hashes are 3105 and 3135 for list1 and 3104 and 3136 for list2. Both checksums are 6240, so the answer is True even though the lists hold different strings.