Cracking the coding interview q1.8

Source: Internet
Author: User

Assume you have a method issubstring which checks if one word is a substring of another. given two strings, S1 and S2, write code to check if S2 is a rotation of S1 using only one call to issubstring (I. E ., "waterbottle" is a rotation of "erbottlewat ").


Idea: S1 connects S1 to form a new string, and then judges whether S2 is a substring of s1s1.


Public static Boolean isrotation (string S1, string S2) {int Len = s1.length (); /* Check that S1 and S2 are equal length and not empty */If (LEN = s2.length () & Len> 0) {/* concatenate S1 and S1 within new buffer */string s1s1 = S1 + S1; return issubstring (s1s1, S2);} return false ;}


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: 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.