判断字符串s2能否由s1旋转得到

/*
 * 判断s2能否由s1旋转得到
 * 已知isSunString函数,但只能调用一次
 * 思路:s1=xy; s2=yx;令s1=s1+s1=xyxy;则s2必为s1的子串
*/
import java.util.Scanner;
public class RotateString {
	static boolean isSubString(String s1,String s2){
		//也可以用KMP来判断,暂时写不出来额
		for(int i=0;i<s1.length()/2;i++){
			for(int j=i+s2.length();j<s1.length();j++){
				//public String substring(int beginIndex, int endIndex)
				//返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。
				//因此,该子字符串的长度为 endIndex-beginIndex。
				String subStr=s1.substring(i,j);
				if(subStr.equals(s2)) return true;
			}
		}
		return false;
	}

	public static void main(String[] args) {
		String s1,s2;
		System.out.println("Please input s1:");
		Scanner cin=new Scanner(System.in);
		s1=cin.next();
		s1=s1+s1;

		while(true){
			System.out.println("Please input a string:");
			s2=cin.next();
			if(s2.equals("0")) break;

			if(s2.length()!=s1.length()/2){
				System.out.println("No");
				continue;
			}

			if(isSubString(s1,s2)){
				System.out.println("Yes");
			}
			else{
				System.out.println("No");
			}
		}
	}
}
/*Test
Please input s1:
waterbottle
Please input a string:
erbottlewat
Yes
Please input a string:
water
No*/

判断字符串s2能否由s1旋转得到

时间: 2024-08-02 10:43:39

判断字符串s2能否由s1旋转得到的相关文章

经典算法面试题目-判断s2是否是s1的旋转字符串(1.8)

题目 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 &qu

将字符串s1中的任何与字符串s2中字符匹配的字符都删除

编写一个程序,将字符串s1中的任何与字符串s2中字符匹配的字符都删除. 函数原型:void my_squeeze(char s1[], char s2[]) #include <stdio.h> void my_squeeze(char s1[], char s2[]) { int i = 0; int j = 0; while (s2[j]) { while(s1[i]) { if (s2[j]==s1[i]) { while (s1[i+1]) { s1[i] = s1[i + 1]; i

返回指定字符串位置的函数FIELD(S,S1,S2,...) 与 FIND_IN_SET(S1,S2) 函数

FIELD(S,S1,S2,...)  与 FIND_IN_SET(S,S1) 函数  ------> 这2个函数都是返回指定字符串在源串中的出现的位置(皆是第一次出现的位置),但2个函数的参数不一样,前者是以列表形式的参数,后者是整体为一个整串的参数.(上边S皆为指定串,前者S1和S2为串列表,后者S1为一个整串) FIELD(S,S1,S2,...) : 返回指定字符串位置的函数FIELD(S,S1,S2,...) FIELD(S,S1,S2,...)返回字符串s在列表s1,s2,.....

Valid Palindrome ——判断字符串是否为回文串

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41488377 Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama&

ACM判断字符串”相等“

Description Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are calledequivalent in one of the two cases: They are equal. If we split string a into two halves of the same siz

怎么判断字符串a的内容包含字符串b的内容

在vb.net可以使用InStr(a,b)方法来判断字符串a的内容是否包含字符串b的内容. InStr(a, b) 如果a中包含有b则InStr(a, b) 返回一个大于0的值,如果不含有b则返回0.可以通过返回值来判断. c#中可以使用 IndexOf语句 public class Example { public static void Main() { string s1 = "ani\u00ADmal"; string s2 = "animal"; // F

判断字符串是否一样

.TXT  .txt  .TxT  为一样的 SameText CompareStr function SameText(const S1, S2: string): Boolean;    if SameText('.txt','.tXT') then     ShowMessage('Same'); 来自为知笔记(Wiz) 判断字符串是否一样

startsWith(),endsWith()的作用,用法,判断字符串a 是不是以字符串b开头或结尾

Java代码 startsWith: if(a.startsWith(b)) //判断字符串a 是不是以字符串b开头. endsWith: if(a.endsWith(b)) //判断字符串a 是不是以字符串b结尾. JAVA例子 1. public class StringDemo{ public class startsWith { public static void main(String args[]){ String s1="this is my startsWith string&

判断字符串是不是数字

NumberUtils.isNumber(str)判断字符串是不是数字或者能不能转换成数字 public class StringIsNumber { public static void main(String[] args) { Scanner s = new Scanner(System.in); String str = s.nextLine(); if(NumberUtils.isNumber(str)){ System.out.println("输入的是数字"); }els