Q1.8 Check if s2 is a rotation of s1
来源:互联网 发布:黄山 知乎 编辑:程序博客网 时间:2024/05/19 20:37
Q: Assume you have a method isSubstring which checks if one word is asubstring of another. Given two strings, s1 and s2, write code tocheck if s2 is a rotation of s1 using only one call to isSubstring (i.e., “waterbottle” is a rotation of “erbottlewat”).
A:
s = xy, 其中x、y分别是子串, 旋转之后s' = yx , 可以发现无论从什么位置开始旋转, yx永远是xyxy是的子串。
#include <iostream>#include <string>using namespace std;bool isSubstring(string s, string t) {if (s.find(t) != string::npos) return true;else return false;}bool isRotation(string s1, string s2) {if (s1.length() != s2.length() || s1.length() < 1) {return false;}return isSubstring(s1+s1, s2);}int main() {string s1 = "waterbottle"; string s2 = "erbottlewat"; cout<<isRotation(s1, s2)<<endl;}
0 0
- Q1.8 Check if s2 is a rotation of s1
- 【6】Decide if one string is a rotation of another string or not
- cci-Q1.8 调用一次子字符串判断确认s2是否为s1的旋转字符
- How To: Check if a user is part of a SharePoint group in InfoPath
- string rotation (if string is the rotate of the other)
- Check if a string value is numeric
- Check if a linked list is palindrome
- wiggle sort Given a list of integers, sort them so the output is s1 <= s2 >=s3 <=s4 ... sN.
- Ten Ways to Check if an Integer Is a Power Of Two in C
- Write an Efficient Method to Check if a Number is Multiple of 3
- Check if a binary tree is subtree of another binary tree
- Ten Ways to Check if an Integer Is a Power Of Two in C
- Ten Ways to Check if an Integer Is a Power Of Two in C
- Check if tree b is part of tree a JAVA 实现
- Q2.7 Check if a list is a palindrome
- Write an Efficient Method to Check if a Number is Multiple of 3(快速判断3的倍数)
- Check if a File is in Internet Explorers Cache
- Check if a given Binary Tree is SumTree
- solr开发案列
- JS控制浮动代码
- Go Web服务开发入门(一) -- 搭建简单web服务器
- 筛选法构造素数表
- IOS从相册上传照片
- Q1.8 Check if s2 is a rotation of s1
- 快速搭建Web环境 Angularjs + Express3 + Bootstrap3
- opencv 3.0 windows 计时函数 timer
- STM32 学习十 Flash下载与调试
- Delphi 与 C/C++ 数据类型对照表(转)
- getline基本用途
- Android自定义音乐播放器
- Java泛型数组转换处理
- 排序算法