题解 | #字符串字符匹配#
字符串字符匹配
https://www.nowcoder.com/practice/22fdeb9610ef426f9505e3ab60164c93
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String s1 = in.nextLine();
String s2 = in.nextLine();
// 1、双指针s1与s2指向S与T,逐一判断s1,若当对应位置的元素在T中,则指针i++,且指针j回到起始位置(防止有重复的元素),否则,指针j++
int i = 0,j = 0;
while(i < s1.length() && j < s2.length()){
if (s1.charAt(i) == s2.charAt(j)){
i++;
j = 0;
}else{
j++;
}
}
if (j >= s2.length()){
System.out.println(false);
}else{
System.out.println(true);
}
}
}
}

查看3道真题和解析