码迷,mamicode.com
首页 > 其他好文 > 详细

Cracking the Coding Interview Q1.8

时间:2014-07-08 21:44:23      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   div   new   

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

标签:style   blog   color   io   div   new   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!