您的位置:首页 > 其它

《Cracking the Coding Interview》——第1章:数组和字符串——题目8

2014-03-18 02:15 543 查看
2014-03-18 02:12

题目:判断一个字符串是否由另一个字符串循环移位而成。

解法:首先长度必须相等。然后将第一个串连拼两次,判断第二个串是否在这个连接串中。

代码:

// 1.8 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”).
#include <cstdio>
#include <cstring>
using namespace std;

class Solution {
public:
bool isStringRotation(char *s1, char *s2) {
if (s1 == nullptr || s2 == nullptr) {
return false;
}

int len1, len2;

len1 = strlen(s1);
len2 = strlen(s2);
if (len1 != len2) {
return false;
}

const int MAXLEN = 1005;
static char tmp[MAXLEN];

tmp[0] = 0;
strcat(tmp, s1);
strcat(tmp, s1);
return strstr(tmp, s2) != nullptr;
}
private:
bool isSubstring(char *haystack, char *needle) {
if (haystack == nullptr || needle == nullptr) {
return false;
}

return strstr(haystack, needle) != nullptr;
}
};

int main()
{
char s1[1005];
char s2[1005];
Solution sol;

while (scanf("%s%s", s1, s2) == 2) {
printf("\"%s\" is ", s2);
if (!sol.isStringRotation(s1, s2)) {
printf("not ");
}
printf("a rotation of \"%s\".\n", s1);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: