首页 > 其他 > 详细

Cracking the Coding Interview Q1.8

时间:2014-07-08 21:44:02      阅读:372      评论:0      收藏: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”).

 

思路:s1连接s1构成新字符串,然后判断s2是否是s1s1的子串即可。

 

    public static boolean isRotation(String s1, String s2) {
        int len = s1.length();
        /* check that s1 and s2 are equal length and not empty */
        if (len == s2.length() && len > 0) { 
            /* concatenate s1 and s1 within new buffer */
            String s1s1 = s1 + s1;
            return isSubstring(s1s1, s2);
        }
        return false;
    }

 

Cracking the Coding Interview Q1.8,布布扣,bubuko.com

Cracking the Coding Interview Q1.8

原文:http://www.cnblogs.com/jdflyfly/p/3830595.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!