





'''simple docstring'''

__A	: Optional[Any]              =       2_56
# Modulus to hash a string
__A	: Optional[Any]              =       1_00_00_03





def 			lowerCAmelCase_						(    a					:					Optional[int]   , a					:					int				):
							a__            =							len(__lowerCAmelCase				)
							a__            =							len(__lowerCAmelCase				)
							if p_len > t_len:
														return False

							a__            =							0
							a__            =							0
							a__            =							1

							# Calculating the hash of pattern and substring of text
							for i in range(__lowerCAmelCase				):
														a__            =							(ord(pattern[i]				) + p_hash * alphabet_size) % modulus
														a__            =							(ord(text[i]				) + text_hash * alphabet_size) % modulus
														if i == p_len - 1:
																					continue
														a__            =							(modulus_power * alphabet_size) % modulus

							for i in range(0   , t_len - p_len + 1				):
														if text_hash == p_hash and text[i : i + p_len] == pattern:
																					return True
														if i == t_len - p_len:
																					continue
														# Calculate the https://en.wikipedia.org/wiki/Rolling_hash
														a__            =							(
														    (text_hash - ord(text[i]				) * modulus_power) * alphabet_size
														    + ord(text[i + p_len]				)
														) % modulus
							return False





def 			lowerCAmelCase_						(    ):
							a__            =							'abc1abc12'
							a__            =							'alskfjaldsabc1abc1abc12k23adsfabcabc'
							a__            =							'alskfjaldsk23adsfabcabc'
							assert rabin_karp(__lowerCAmelCase   , __lowerCAmelCase				) and not rabin_karp(__lowerCAmelCase   , __lowerCAmelCase				)

							# Test 2)
							a__            =							'ABABX'
							a__            =							'ABABZABABYABABX'
							assert rabin_karp(__lowerCAmelCase   , __lowerCAmelCase				)

							# Test 3)
							a__            =							'AAAB'
							a__            =							'ABAAAAAB'
							assert rabin_karp(__lowerCAmelCase   , __lowerCAmelCase				)

							# Test 4)
							a__            =							'abcdabcy'
							a__            =							'abcxabcdabxabcdabcdabcy'
							assert rabin_karp(__lowerCAmelCase   , __lowerCAmelCase				)

							# Test 5)
							a__            =							'Lü'
							a__            =							'Lüsai'
							assert rabin_karp(__lowerCAmelCase   , __lowerCAmelCase				)
							a__            =							'Lue'
							assert not rabin_karp(__lowerCAmelCase   , __lowerCAmelCase				)
							print('Success.'				)


if __name__ == "__main__":
		test_rabin_karp()










_lowerCamelCase    :		Optional[Any]		      =    256
# Modulus to hash a string
_lowerCamelCase    :		Optional[Any]		      =    1_000_003

def        __a			(   __lowerCAmelCase      ,	__lowerCAmelCase   )     ->     bool:
					SCREAMING_SNAKE_CASE							:     Tuple						=				len(__lowerCAmelCase   )
					SCREAMING_SNAKE_CASE							:     Tuple						=				len(__lowerCAmelCase   )
					if p_len > t_len:
										return False

					SCREAMING_SNAKE_CASE							:     Dict						=				0
					SCREAMING_SNAKE_CASE							:     Tuple						=				0
					SCREAMING_SNAKE_CASE							:     Optional[Any]						=				1

					# Calculating the hash of pattern and substring of text
					for i in range(__lowerCAmelCase   ):
										SCREAMING_SNAKE_CASE							:     Tuple						=				(ord(pattern[i]   ) + p_hash * alphabet_size) % modulus
										SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				(ord(text[i]   ) + text_hash * alphabet_size) % modulus
										if i == p_len - 1:
															continue
										SCREAMING_SNAKE_CASE							:     Optional[Any]						=				(modulus_power * alphabet_size) % modulus

					for i in range(0      ,	t_len - p_len + 1   ):
										if text_hash == p_hash and text[i : i + p_len] == pattern:
															return True
										if i == t_len - p_len:
															continue
										# Calculate the https://en.wikipedia.org/wiki/Rolling_hash
										SCREAMING_SNAKE_CASE							:     int						=				(
										    (text_hash - ord(text[i]   ) * modulus_power) * alphabet_size
										    + ord(text[i + p_len]   )
										) % modulus
					return False

def        __a			(   )     ->     None:
					SCREAMING_SNAKE_CASE							:     Any						=				'abc1abc12'
					SCREAMING_SNAKE_CASE							:     str						=				'alskfjaldsabc1abc1abc12k23adsfabcabc'
					SCREAMING_SNAKE_CASE							:     List[str]						=				'alskfjaldsk23adsfabcabc'
					assert rabin_karp(__lowerCAmelCase      ,	__lowerCAmelCase   ) and not rabin_karp(__lowerCAmelCase      ,	__lowerCAmelCase   )

					# Test 2)
					SCREAMING_SNAKE_CASE							:     List[str]						=				'ABABX'
					SCREAMING_SNAKE_CASE							:     int						=				'ABABZABABYABABX'
					assert rabin_karp(__lowerCAmelCase      ,	__lowerCAmelCase   )

					# Test 3)
					SCREAMING_SNAKE_CASE							:     int						=				'AAAB'
					SCREAMING_SNAKE_CASE							:     Tuple						=				'ABAAAAAB'
					assert rabin_karp(__lowerCAmelCase      ,	__lowerCAmelCase   )

					# Test 4)
					SCREAMING_SNAKE_CASE							:     Tuple						=				'abcdabcy'
					SCREAMING_SNAKE_CASE							:     Optional[Any]						=				'abcxabcdabxabcdabcdabcy'
					assert rabin_karp(__lowerCAmelCase      ,	__lowerCAmelCase   )

					# Test 5)
					SCREAMING_SNAKE_CASE							:     List[Any]						=				'Lü'
					SCREAMING_SNAKE_CASE							:     Optional[Any]						=				'Lüsai'
					assert rabin_karp(__lowerCAmelCase      ,	__lowerCAmelCase   )
					SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				'Lue'
					assert not rabin_karp(__lowerCAmelCase      ,	__lowerCAmelCase   )
					print('Success.'   )


if __name__ == "__main__":
			test_rabin_karp()