关闭

crack the code interview 1.8

226人阅读 评论(0) 收藏 举报
分类:
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”)

int length(char * str)
{
    if (str == NULL)
        return 0;
    int length = 0;
    char * start = str;
    while (*start != '\0')
    {
        length ++;
        start ++;
    }
    return length;
}

bool isRotation(char * c1, char * c2)
{
    if (c1 == NULL)
        return false;
    if (c2 == NULL)
        return true;
    int len = length(c1);
    char * dc1 = new char[len * 2 + 1];
    for (int i = 0; i < len; i++)
    {
        dc1[i] = c1[i];
        dc1[i + len] = c1[i];
    }
    dc1[len * 2] = '\0';
    return isSubstring(dc1, c2);
} 


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:25180次
    • 积分:753
    • 等级:
    • 排名:千里之外
    • 原创:54篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条