字符串旋转检测算法
来源:互联网 发布:上海银行淘宝金卡6510 编辑:程序博客网 时间:2024/05/21 08:54
题目:
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”).
解决思路:
1. Check if length(s1) == length(s2). If not, return false.
2. Else, concatenate s1 with itself and see whether s2 is substring of the result.
- input: s1 = apple, s2 = pleap ==> apple is a substring of pleappleap
- input: s1 = apple, s2 = ppale ==> apple is not a substring of ppaleppale
源码如下:
#include <iostream>#include <string>using namespace std;bool isSubstring(string s1, string s2){ if(s1.find(s2) != string::npos) return true; else return false;}bool isRotation(string s1, string s2){ if(s1.length() != s2.length() || s1.length()<=0) return false; return isSubstring(s1+s1, s2);}int main(){ string s1 = "apple"; string s2 = "pleap"; cout<<isRotation(s1, s2)<<endl; cout<<string::npos<<endl; return 0;}
0 0
- 字符串旋转检测算法
- 旋转字符串算法由浅入深
- 算法 旋转字符串
- 【算法】旋转字符串
- 【算法】旋转字符串
- 旋转字符串算法由浅入深
- 算法:字符串旋转
- 算法-字符串旋转
- 算法之旋转字符串
- 字符串旋转算法 -- 求逆
- 算法十二----左旋转字符串
- 有趣的算法-旋转字符串
- 算法中字符串系列之旋转字符串
- 算法整理-字符串(LCS,旋转字符串)
- 算法面试-学习笔记-左旋转字符串
- 对旋转字符串算法的理解
- 算法之左旋转字符串(一)
- 算法之道--左右旋转字符串
- 编写高效的C#图像处理程序(3) Rgb=>Lab,图像缺陷检测的例子
- Lisp的本质
- 地产公司新建商业中心调研-调研内容
- 基本寄存器
- 浙大PAT 1007. 素数对猜想 (解题思路)
- 字符串旋转检测算法
- 编写高效的C#图像处理程序(4) Rgb=>Lab,C# vs. C++(VC)
- 顺时针打印矩阵
- fedora 14编译安卓内核
- 第二周作业
- jdbctemplate batchUpdate方法。
- pat 1071 Speech Patterns (25)
- mysql : Lock wait timeout exceeded; try restarting transaction
- java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index