原文:

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”).

译文:

假设你有一个isSubstring函数,可以检测一个字符串是否是另一个字符串的子串。 给出字符串s1和s2,只使用一次isSubstring就能判断s2是否是s1的旋转字符串, 请写出代码。旋转字符串:"waterbottle"是"erbottlewat"的旋转字符串。

解答:

如果s1是s2的旋转字符串,那么我们一定能找到一个切割点将s1分割成xy,并且yx等于s2。例如:

s1 = xy = waterbottle
x = wat
y = erbottle
s2 = yx = erbottlewat

那么我们可以知道yx一定是xyxy的一个子串,也就是说,s2一定是s1s1的子串。所以这个题目的解法也就一目了然了。

public class Main {

    public static boolean isSubstring(String s1, String s2) {
if(s1 == null || s2 == null)
return false;
if(s1.contains(s2))
return true;
else
return false;
} public static boolean isRotation(String s1, String s2) {
return isSubstring(s1+s1,s2);
} public static void main(String args[]) {
String s1 = "waterbottle";
String s2 = "erbottlewat";
System.out.println(isRotation(s1,s2));
}
}

Cracking the coding interview--Q1.8的更多相关文章

  1. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  2. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  3. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

  9. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  10. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

随机推荐

  1. linux补包

    1.挂载文件export LANG=Cmkdir -p /media/cdrommount /dev/cdrom /media/cdrommount /dev/hdc /media/cdrommoun ...

  2. iOS APP之本地数据存储(译)

    最近工作中完成了项目的用户信息本地存储,查阅了一些本地存储加密方法等相关资料.期间发现了一个来自印度理工学院(IIT)的信息安全工程师的个人博客,写了大量有关iOS Application secur ...

  3. 对openflow 1.0协议的扩展

    通过这几天对openvswitch代码的分析,以及项目的须要,须要对openflow 1.0进行一定的扩展,发现网上没有这方面的教程,尽管在搞懂ovs代码架构,floodlight controlle ...

  4. 【java】Windows7 下设置环境变量

    Windows 7下配置JDK环境变量參数设置: 1.    安装JDK,安装过程中能够自己定义安装文件夹等信息,比如我们选择安装文件夹为:D:\Program Files (x86)\Java\jd ...

  5. linux grep常用参数

    # grep [-acinv] [--color=auto] '搜寻字符串' filename选项与参数:-c :计算找到 '搜寻字符串' 的次数-i :忽略大小写的不同,所以大小写视为相同-n :顺 ...

  6. JSON 格式化为易读格式的字符串

    算法 http://blog.csdn.net/yanghaitaohhh/article/details/39672783 使用算法如下: 对输入字符串,逐个字符的遍历 1.获取当前字符. 2.如果 ...

  7. rpm命令数据库修复日志

    今天在linux安装软件过程中遇到了一个小坑,rpm数据库被破坏: 状况: #rpm -qa | grep rpm 返回: [解决方案] 删除旧数据库,然后重建数据库: 删除旧数据库: # rm /v ...

  8. innodb的innodb_buffer_pool_size和MyISAM的key_buffer_size

    一. key_buffer_size 对MyISAM表来说非常重要. 如果只是使用MyISAM表,可以把它设置为可用内存的 30-40%.合理的值取决于索引大小.数据量以及负载 -- 记住,MyISA ...

  9. 张羿给的删除重复数据的mssql语句

    select count(1), gsdm, idfrom ods_sc.T_D_DEVICE_COMMONgroup by gsdm, idhaving count(1) > 1; delet ...

  10. Property type 'id<tabBarDelegate>' is incompatible with type 'id<UITabBarDelegate> _Nullable' inherited from 'UITabBar'

    iOS报错:Property type 'id' is incompatible with type 'id _Nullable' inherited from 'UITabBar' 如图: 可能原因 ...