Determine whether s2 can be included in the string obtained by performing cyclic shift (rotate) through s1. s2rotate

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.