题解 | #字符串字符匹配#
字符串字符匹配
https://www.nowcoder.com/practice/22fdeb9610ef426f9505e3ab60164c93
法1:利用contains()方法
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String s1 = in.nextLine();
String s2 = in.nextLine();
for (char a : s1.toCharArray()) {
if (!s2.contains(String.valueOf(a))) {
System.out.println("false");
return;
}
}
System.out.println("true");
}
}
}
法2:双指针
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String s1 = in.nextLine();
String s2 = in.nextLine();
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(i==s1.length())System.out.println("true");
else System.out.println("false");
}
}
}
