[google面试CTCI] 1-8.判断子字符串
【字符串与数组】
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的子串是同样的道理。
代码如下所示:
上面代码中的isSubString是字符串匹配算法,最著名的有KMP算法、BM算法等,其思想及其具体实现,可以查看博客里的另外两篇文章。
作者:Viidiot 微信公众号:linux-code
[google面试CTCI] 1-8.判断子字符串的更多相关文章
- 106、Java中String类之使用contains()方法判断子字符串是否存在
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 105、Java中String类之利用indexOf()方法判断子字符串是否存在
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- [google面试CTCI] 1-4.判断两个字符串是否由相同字符组成
[字符串与数组] Q:Write a method to decide if two strings are anagrams or not 题目:写一个算法来判断两个字符串是否为换位字符串.(换位字 ...
- [google面试CTCI]1-3.字符串去重
[字符串与数组] Q:Design an algorithm and write code to remove the duplicate characters in a string without ...
- [google面试CTCI] 1-5.替换字符串中特定字符
[字符串与数组] Q:Write a method to replace all spaces in a string with ‘%20’ 题目:写一个算法将一个字符串中的空格替换成%20 解答: ...
- [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 ...
- [google面试CTCI] 1-6.图像旋转问题
[字符串与数组] Q:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, wr ...
- el: 在jsp页面内使用函数判断子字符串
e.g. <c:forEach items="${datas}" var="data"> <c:if test="${not fn: ...
- [google面试CTCI] 2-1.移除链表中重复元素
[链表] Q:Write code to remove duplicates from an unsorted linked list FOLLOW UP How would yo ...
随机推荐
- P31RestKit.dll 2.0 Unity3d json
using System.Collections.Generic; using UnityEngine; using System.Collections; using Prime31; public ...
- php禁用一些重要功能
passthru() 功能叙述性说明:我们同意将运行外部程序和回音输出.分类似至 exec(). 临界水平:高 exec() 功能叙述性说明:同意运行外部程序(例如 UNIX Shell 要么 CMD ...
- vim添加自己//解决方案
使用vim从外面将代码复制并粘贴到时间,假设有一排//凝视.自己主动下一行加入//和每行增加一个<tab>.格全乱:其他编辑器*.c *cpp其他文件格当公式,假设有一排//凝视,按o换行 ...
- js拾遗:appendChild 添加移动节点
原文:js拾遗:appendChild 添加移动节点 写js一年多了,一直以为自己很牛逼,开始写各种博文分享,昨天写了一篇<浅谈 IE下innerHTML导致的问题>在看了下面的评论,我才 ...
- Web API 2 对 CORS 的支持
Web API 2 对 CORS 的支持 CORS概念 跨域资源共享 (CORS) 是一种万维网联合会 (W3C) 规范(通常被认为是 HTML5 的一部分),它可让 JavaScript 克服由浏览 ...
- VisualStudio2012轻松把JSON数据转换到POCO的代码
原文:VisualStudio2012轻松把JSON数据转换到POCO的代码 在Visual Studio 2012中轻松把JSON数据转换到POCO的代码,首先你需要安装Web Esse ...
- STL在迭代的过程,删除指定的元素
直接在Code.在 Picture #include <iostream> #include <list> using namespace std; // STL在迭代的过程中 ...
- MVC验证07-自定义Model级别验证
原文:MVC验证07-自定义Model级别验证 在一般的自定义验证特性中,我们通过继承ValidationAttribute,实现IClientValidatable,只能完成对某个属性的自定义验证. ...
- Asp.Net MVC5入门学习系列②
原文:Asp.Net MVC5入门学习系列② 添加一个Controller(控制器) 因为我们用的是Asp.Net MVC,MVC最终还是一套框架,所以我们还是需要遵循它才能玩下去,或者说是更好的利用 ...
- FlexBuilder远程调试WEB应用
Flex使用的开发工具FlexBuiler 3,web server使用apache. 1 第一次安装IE相应flashplayer的debug版本号,下载Flash player 11.8安装,下载 ...