【字符串与数组】

Q: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。书写代码来判断S2是否能通过旋转S1得到。(只能使用一次isSubstring方法)

解答:


以waterbottle和erbottlewat为例,我们只有使用一次isSubstring的机会,但是单单从这两个字符串来看,我们找不到彼此之间的子串关系。因此,可以考虑去改变、构造(几何里的做辅助线)字符串。要判断S2是否能通过旋转S1得到,我们可以先将S1扩展成S1S1,即erbottlewat扩展成erbottlewaterbottlewat,然后再判断S2是否为S1S1的子串。当然,将S2扩展成S2S2,然后再判断S1是否为S2S2的子串是同样的道理。

代码如下所示:

int is_rotation(char* str1,char* str2){ int len1=strlen(str1); int len2=strlen(str2); if(len1!=len2) return 0; char* newStr=malloc(len1*sizeof(char)); strcpy(newStr,str1); strcat(newStr,str1); int rValue; rValue=isSubString(newStr,str2); free(newStr); return rValue; }

上面代码中的isSubString是字符串匹配算法,最著名的有KMP算法、BM算法等,其思想及其具体实现,可以查看博客里的另外两篇文章。


作者:Viidiot  微信公众号:linux-code


[google面试CTCI] 1-8.判断子字符串的更多相关文章

  1. 106、Java中String类之使用contains()方法判断子字符串是否存在

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  2. 105、Java中String类之利用indexOf()方法判断子字符串是否存在

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  3. [google面试CTCI] 1-4.判断两个字符串是否由相同字符组成

    [字符串与数组] Q:Write a method to decide if two strings are anagrams or not 题目:写一个算法来判断两个字符串是否为换位字符串.(换位字 ...

  4. [google面试CTCI]1-3.字符串去重

    [字符串与数组] Q:Design an algorithm and write code to remove the duplicate characters in a string without ...

  5. [google面试CTCI] 1-5.替换字符串中特定字符

    [字符串与数组] Q:Write a method to replace all spaces in a string with ‘%20’ 题目:写一个算法将一个字符串中的空格替换成%20 解答: ...

  6. [google面试CTCI] 1-7.将矩阵中特定行、列置0

    [字符串与数组] Q:Write an algorithm such that if an element in an MxN matrix is 0, its entire row and colu ...

  7. [google面试CTCI] 1-6.图像旋转问题

    [字符串与数组] Q:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, wr ...

  8. el: 在jsp页面内使用函数判断子字符串

    e.g. <c:forEach items="${datas}" var="data"> <c:if test="${not fn: ...

  9. [google面试CTCI] 2-1.移除链表中重复元素

    [链表] Q:Write code to remove duplicates from an unsorted linked list      FOLLOW UP      How would yo ...

随机推荐

  1. ArcGIS 10.1 for Server 扩展开发(SOE)

    原文连接:http://blog.csdn.net/arcgisserver_book/article/details/7869368 第一章为什么使用SOE 在ArcGIS 10.1中ArcGIS ...

  2. 关于Promise的一个案例

    题目:红灯三秒亮一次,绿灯一秒亮一次,黄灯2秒亮一次:如何让三个灯不断交替重复亮灯?(用Promise实现) 解答思路一: function red(){ console.log('red'); } ...

  3. javascript利用map,every,filter,some,reduce,sort对数组进行最优化处理

    案例: var scoresTable=[ {id:11,name:"小张",score:80}, {id:22,name:"小王",score:95}, {i ...

  4. WebBrowser!

    原文:WebBrowser! 我现在先放一些基础的文章在这里,以后再放别的上来官方范例连接http://www.microsoft.com/china/msdn/library/langtool/vc ...

  5. UVA - 11986 Save from Radiation

    Description J Save from Radiation Most of you are aware of Nuclear Power Plant Explosion at Fukushim ...

  6. NPOI mvc easyui 根据Excel模板 生成Excel

    1.首先下载 NPOI  https://npoi.codeplex.com/releases  只要dll 就好 示例代码库太难懂了. NPOI 是一个开源  免费的 东西.而且不依赖 office ...

  7. SHDocVw, AxSHDocVw的引用

    原文:SHDocVw, AxSHDocVw的引用 SHDocVw的引用SHDocVw一定要在下面这个路径找: 类似 C:\Program Files\Microsoft Visual Studio 9 ...

  8. Ubuntu 14.04 编译newLISP 10.6.0

    1. 确保安装了gcc4.8.2 2. 安装须要的库: apt-get install libreadline6 libreadline6-dev 3. 下载并解压newLISP源码,这个不多说了,去 ...

  9. qml能够这么玩

    Qt 5以后qmlscene被qml所替代,/usr/bin/qml能够用来执行.qml文件.所以,我们就能够和sh一样的来写界面了. #!/usr/bin/env qml import QtQuic ...

  10. angular.js的一点理解

    对angular.js的一点理解 2015-01-14 13:18 by MrGeorgeZhao, 317 阅读, 4 评论, 收藏, 编辑 最近一直在学习angular.js.不得不说和jquer ...