crack the code interview 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”)

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);
} 


阅读更多
个人分类: crack the code interview
上一篇人群与社会——第三周
下一篇crack the code interview 2.1
想对作者说点什么? 我来说一句

crack over the code interview

2013年10月17日 2.05MB 下载

Crack over the code interview

2014年11月03日 2.5MB 下载

没有更多推荐了,返回首页

关闭
关闭