Determine whether s2 can be included in the string obtained by performing cyclic shift (rotate) through s1. s2rotate
Problem: Given two strings s1 and s2, it is required to determine whether s2 can be contained by the string obtained through s1 cyclic shift (rotate. For example, if S1 = AABCD and s2 = CDAA, true is returned. If s1 = ABCD and s2 = ACBD are given, false is returned.
Here is an idea about the inclusion of string shifting (the beauty of programming)
Reference original
"
Solution 2: We can also analyze the results after the cyclic shift.
Take S1 = ABCD as an example. analyze the result after cyclic shift of S1, as shown below:
ABCD ---> BCDA ----> CDAB ----> DABC ----> ABCD ......
If we keep the previously removed data, we will find the following rule:
ABCD ---> ABCDA ----> ABCDAB ----> ABCDABC ----> ABCDABCD ......
Therefore, we can see that all the strings produced by the cyclic shift of S1 will be substrings of the S1S1 string. If S2 can be obtained from S1 cyclic shift, S2 must be on S1S1, reducing the time complexity.
"
The Code is as follows: why not encapsulate it? You guess.
1 static void Main(string[] args) 2 { 3 4 String s1="ABDDE"; 5 String s2="DDAB"; 6 string s3=string.Concat(s1,s1); 7 if (s3.Contains(s2)) 8 { 9 Console.WriteLine("true"); 10 }else{11 Console.WriteLine("false"); 12 } 13 Console.ReadKey();14 15 }
------ Once again standing on the shoulders of giants