Checking if two strings are rotationally equivalent means determining if one string can be obtained from the other by rotating the characters. For example, the strings "abcde" and "cdeab" are rotationally equivalent.
Here's how to check this in Python:
Determine if two given strings, str1
and str2
, are rotationally equivalent.
One of the most straightforward techniques involves concatenating one string to itself and then checking if the other string is a substring of this concatenated string.
str1 = "abcde" str2 = "cdeab" def are_rotationally_equivalent(str1, str2): if len(str1) != len(str2): return False return str2 in str1 + str1 print(are_rotationally_equivalent(str1, str2)) # Output: True
Another method involves rotating str1
iteratively and checking if any of these rotations match str2
.
str1 = "abcde" str2 = "cdeab" def are_rotationally_equivalent(str1, str2): if len(str1) != len(str2): return False for i in range(len(str1)): if str1[i:] + str1[:i] == str2: return True return False print(are_rotationally_equivalent(str1, str2)) # Output: True
str1
.Choose the method that best fits the specific requirements and constraints of your problem!
terraform sqlanywhere android-radiobutton string-formatting hyperledger mariadb signalr-hub statusbar timeoutexception rerender