[CareerCup] Arrays and Strings-Q1.7

Source: Internet
Author: User

Reprinted please indicate the source: http://write.blog.csdn.net/postlist/0/all/draft

 

 

Question:

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 ").

Translation:

Assume that you have an isSubstring function that can judge whether a string is an isSubstring function of another string substring. For the given strings s1 and s2, write a program, the isSubstring can be called only once to determine whether s2 is a s1 rotating string. For example, "waterbottle" is a rotating string of "erbottlewat.

 

Translation:

Here, we will slightly add that rotating a string refers to moving the first (next) character of a string to the back (Front ).

IsSubstring is required in the question and can only be used once. To use this function, the length of the two strings cannot be the same. You cannot directly use this function for s1 and s2. To rotate a string, you only need to move the string to the front or to the back. Therefore, you can move s1 + s1 and then determine whether s2 is a substring of s1 + s1. If yes, it is proved that s2 is the rotating string of s1. Above "erbottlewat" + "erbottlewat" = "erbottleWatErbottleWat ", it is obvious that" waterbottle "is its substring (the bold section above ).

Implementation Code:

 

bool isRotate(string s1,string s2){if(s1.length() != s2.length())return false;return isSubstring(s1+s1,s2);}
The implementation code is simple, and the isSubstring function can be implemented using the KMP algorithm. We will not test it here!

 

Next let's look at the problem around rotating strings:

If a character string is known, it is rotated to the end of the string before the k-digit (starting from 0), and the rotated string is obtained. For example, for the string "abcdef", rotate the character before the 2nd position to the end to get "cdefab ". Requires that the space complexity be O (1) and the time complexity be O (n)

Ideas:

It is easy to think of another way to open up an array of characters with the length of len + 1 (the last one is used to save '\ 0 '), copy the character before the k-bit of the original string to the end of the character array, and copy the character after the k-bit (including the k-bit) to the front of the character array, in this case, the time complexity is O (n), and the space complexity is O (n ).

In the question, the space complexity is O (1), that is, the rotation must be carried out in the same place. We can do this by first reversing the two characters that need to be rotated, then, the entire string is reversed to obtain the desired rotating string. For example, if "abcdef" is to be rotated to get "cdefab", we can first reverse "AB" and "cdef" to get "bafedc ", then reverse "bafedc" to obtain the required rotation string "cdefab ".

 

Implementation Code:

 

/*************************************** **************************************** * ******** Title Description: if a character string is known, it is rotated to the end of the string before the k-digit (starting from 0), and the rotated string is obtained. For example, for the string "abcdef", rotate the character before the 2nd position to the end to get "cdefab ". Date: 2014-03-19 *************************************** **************************************** * *********/# include
 
  
# Include
  
   
/* Reverse the character between start and end of string str */void reverseString (char * str, int start, int end) {while (start <end) {str [start] = str [start] + str [end]; str [end] = str [start]-str [end]; str [start] = str [start]-str [end]; start ++; end -- ;}}/* reverse three times */void rotate (char * str, int k) {int len = strlen (str); if (k <= 0 | len <= k) return; reverseString (str, 0, k-1); reverseString (str, k, len-1); reverseString (str, 0, len-1);} int main () {char str1 [] = "abcdef"; char str2 [] = "abcdefghket "; rotate (str1, 1); rotate (str2, 4); puts (str1); puts (str2); return 0 ;}
  
 
Test results:

 

 

 

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.