use isSubstring to check if one word is a rotation of another.
1: /// <summary>
2: /// Assume you have a method isSubstring which checks if one word is a substring of another.
3: /// Given two strings, s1 and s2,
4: /// write code to check if s2 is a rotation of s1 using only one call to
5: /// isSubstring (i.e., “waterbottle” is a rotation of “erbottlewat”).
6: /// </summary>
7: class Program
8: {
9: static void Main(string[] args)
10: {
11: string s1 = "waterbottle";
12: string s2 = "erbottlewat";
13: Program p = new Program();
14: bool r = p.IsARotation(s1, s2);
15: }
16:
17: public bool IsARotation(string s1, string s2)
18: {
19: if (string.IsNullOrEmpty(s1) || string.IsNullOrEmpty(s2))
20: {
21: throw new ArgumentNullException("please do not input empty or null string");
22: }
23:
24: if (s1.Length != s2.Length)
25: {
26: return false;
27: }
28:
29: string ns = s1 + s1;
30:
31: return ns.Contains(s2);
32: }
33: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
use isSubstring to check if one word is a rotation of another.的更多相关文章
- 【LeetCode OJ】Word Ladder I
Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in t ...
- LeetCode题解:(139) Word Break
题目说明 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, dete ...
- [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- 11-10 CC150第一章
题目: 1.1 Implement an algorithm to determine if a string has all unique characters. What if you can n ...
- [CareerCup] 1.8 String Rotation 字符串的旋转
1.8 Assume you have a method isSubstring which checks if one word is a substring of another. Given t ...
- Cracking the coding interview--Q1.8
原文: Assume you have a method isSubstring which checks if one word is a substring of another. Given t ...
- CCI_chapter 1
1.1Implement an algorithm to determine if a string has all unique characters What if you can not us ...
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
- CareerCup它1.8 串移包括问题
[称号] 原文: 1.8 Assume you have a method isSubstring which checks if one word is a substring of another ...
随机推荐
- Andriod 中常见错误
1.Open quote is expected for attribute "android:name" associated with an element type &quo ...
- ubuntu intelliJ IDEA 12.1.4 安装
1 php插件 http://plugins.jetbrains.com/plugin/?id=6610 把插件下载到一个目录下,如果插件不兼容,多试几个版本! 2 打开ide, FILE -> ...
- jQuery如何检查某个元素在网页上是否存在
$("ID")获取的永远是对象,即使网页上没有此元素.因此当要用jQuery检查某个元素在网页上是否存在时,不能使用以下代码: if($("#ID")){ // ...
- jsp 嵌套iframe 从iframe中表单提交并传值到外层
今天因需求迭代 更改元来代码 遇到了这么个问题 就是想在 iframe中提交后进行整个页面的跳转 并把iframe中的值传到外层jsp 大概就是这个样子 外层 a.jsp <div id=&qu ...
- Spring MVC框架理解
原文链接:ITeye SpringMVC深度探险专栏 基本要素 1. 指定SpringMVC的入口程序(在web.xml中) <!-- Processes application request ...
- 在windows下的mysql使用
具体可参照http://jingyan.baidu.com/article/3aed632e19b5e8701080918f.html 1.安装mysql. 直接百度搜索mysql下载.
- 如何让U盘支持大于4G的文件
U盘通常是FAT(*)格式,不能支持大于4G的文件.为了实现这个目的,通常可以把U盘格式化成NTFS或者exFAT,这两种文件系统都支持大于4G的文件. 一.格式化成NTFS第一步首先我们把优盘插入电 ...
- 使用ajax传递及接收数据
前端代码: <input id="txtNum1" name="txtNum1" type="text" width="13 ...
- [BZOJ 1009] [HNOI2008] GT考试 【AC自动机 + 矩阵乘法优化DP】
题目链接:BZOJ - 1009 题目分析 题目要求求出不包含给定字符串的长度为 n 的字符串的数量. 既然这样,应该就是 KMP + DP ,用 f[i][j] 表示长度为 i ,匹配到模式串第 j ...
- uva 11437 - Triangle Fun
计算几何: 直线交点: #include<cstdio> using namespace std; struct node { double x,y; node(,):x(x),y(y){ ...