Given two strings, str1 and str2, check if str2 can be obtained by rotating str1 exactly 2 places in either a clockwise or anticlockwise direction.
Rotate the string 1. Clockwise: Move frist two characters in string 1 to the end. Anticlockwise: Move last two characters in string 1 to the front. Check if either of the rotated versions of string 1 matches string 2 exactly.
str1 = "river" str2 = "verri" l = len(str1) r1 = str1[2:] + str1[0:2] r2 = str1[-2:] + str1[0:-2] print(r1 == str2 or r2 == str2)
const str1 = "river"; const str2 = "verri"; const len = str1.length; const r1 = str1.slice(2) + str1.slice(0, 2); const r2 = str1.slice(len - 2) + str1.slice(0, len - 2); console.log(r1 === str2 || r2 === str2);