【字符串与数组】

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. POJ 1088 滑雪 (动规)

    滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 75664 Accepted: 28044 Description Mich ...

  2. 【转】UIAutomator定位Android控件的方法实践和建议(Appium姊妹篇)

    原文地址:http://blog.csdn.net/zhubaitian/article/details/39777951 在本人之前的一篇文章<<Appium基于安卓的各种FindEle ...

  3. sql 给表结构增加说明

    create  proc proc_addReMark    @TableName nvarchar(50),     @RowName nvarchar(50),    @RowReMark  nv ...

  4. 多线程学习之五超时模式Timer

    timed[超时模式]案例:一个线程提供下载数据,另一个线程执行下载,如果有5秒钟以上,提供下载的线程没有提供数据,下载线程因超时异常,停止下载线程运行. 超时异常类 /** * */ package ...

  5. 使用JavaCompiler编译java源文件

    从1.6版本的JDK开始,JDK提供了标准的包可以方便的调用JVM的编译器,可以方便的使用JVM的编译器来编译java源文件.JDK提供的调用接口是JavaCompiler类,该类在JDK的tools ...

  6. Oracle 11g 客户端的安装和配置。

    原文:Oracle 11g 客户端的安装和配置. 数据库和客户端在不同的机器之上. 在安装之前,在安装Oracle数据库的服务器上导航到下面的目录. 将listener.ora和tnsnames.or ...

  7. 4. SQL Server数据库状态监控 - 作业状态

    原文:4. SQL Server数据库状态监控 - 作业状态 有很多地方可以设置定时任务,比如:Windows的计划任务,Linux下的crontab,各种开发工具里的timer组件.SQL Serv ...

  8. ASP.NET 5 Overview

    ASP.NET 5概观 (ASP.NET 5 Overview) http://www.asp.net/vnext/overview/aspnet-vnext/aspnet-5-overview AS ...

  9. Invent 2014回顾

    AWS re:Invent 2014回顾   亚马逊在2014年11月11-14日的拉斯维加斯举行了一年一度的re:Invent大会.在今年的大会上,亚马逊一股脑发布和更新了很多服务.现在就由我来带领 ...

  10. leetcode第五题--Longest Palindromic Substring

    Problem:Given a string S, find the longest palindromic substring in S. You may assume that the maxim ...