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 ...
随机推荐
- ffmpeg只使用h264编译参数
--disable-everything --enable-decoder=h264 --enable-demuxer=h264 --enable-parser=h264 --disable-ffpl ...
- wamp配置
# #localhost # <VirtualHost *:> DocumentRoot "D:/software/wamp/www" ServerName local ...
- 防止apche列出目录以及下载文件
1.修改httpd.conf,将override none改为override all 2.在需要设置权限的目录上传.htaccess文件,.htaccess文件内容如下: <FilesMatc ...
- mysql 清空表 Truncate及delete区别
1.delete from 表名[where]; 2.truncate table 表名; 3.delete将mysql表中所有记录一条一条删除到删完 4.truncate保留mysql表的结构,重新 ...
- 在ubuntu中获得root权限
在终端中输入:(1)sudo passwd rootEnter new UNIX password: (在这输入你的密码)Retype new UNIX password: (确定你输入的密码)pas ...
- 【web安全】第五弹:burpsuite proxy模块的一些理解
作为一只小小小白的安全新手,只会简单的用sqlmap扫扫网站,用burpsuite的proxy模块拦截一些请求.最近又对proxy有点儿小理解,记录之. 1. 查看sqlmap注入的语句以及HTTP ...
- CSS3------background-size(背景图片尺寸属性)
background-size 可以设置背景图片的大小,数值包括 长度length和百分比percentage. 并且会根据背景原点位置 background-origin 设置其图片覆盖的范围.那么 ...
- Phonegap之内存问题
使用phonegap的拍照功能时,安卓机会出现崩溃现象,这一问题的原因也许是你的手机内存不足,实际上却不是phonegap的问题,它也是原生android apps的一个普遍问题. 产生这一问题是因为 ...
- Java多线程初学者指南(11):使用Synchronized块同步方法
synchronized关键字有两种用法.第一种就是在<使用Synchronized关键字同步类方法>一文中所介绍的直接用在方法的定义中.另外一种就是synchronized块.我们不仅可 ...
- 一个Bootstrap风格的分页控件
http://www.cnblogs.com/wangwei123/p/3682626.html 主题 jQueryBootstrap 一个Bootstrap风格的分页控件,对于喜欢Bootstr ...