"Programmer interview book", "Programmer interview book"
[Disclaimer: All Rights Reserved. indicate the source for reprinting. Do not use it for commercial purposes. Contact mailbox: libin493073668@sina.com]
Question link: http://www.nowcoder.com/practice/bc12808a2b0f445c96a64406d5513e96? Rp = 1 & ru =/ta/cracking-the-coding-interview & qru =/ta/cracking-the-coding-interview/question-ranking
Description
Suppose we all know very efficient algorithms to check whether a word is a substring of another string. Compile this algorithm into a function. If two strings s1 and s2 are specified, compile the code to check whether s2 is rotated by s1. You must call the function that checks the substring only once.
Given two strings s1 and s2, return the bool value to indicate whether s2 is rotated by S1. The string contains letters, spaces, and case-sensitive characters. The length of the string is less than or equal to 1000.
Test example:
"Hello world", "worldhello"
Return Value: false
"Waterbottle", "erbottlewat"
Return Value: true
Ideas
For this question, we only need to copy and splice the original string to obtain a new string, and then determine whether the reverse string is a substring of the new string.
Class ReverseEqual {public: bool checkReverseEqual (string s1, string s2) {// write code hereif (s1.length ()! = S2.length () return false; string s3 = s1 + s1; return s3.find (s2 )! =-1 ;}};
Copyright Disclaimer: This article is the original article of the blogger. If it is reproduced, please indicate the source