题解 | #字符串字符匹配#
字符串字符匹配
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 的区别 String str1 = in.next(); String str2 = in.next(); int n = str1.length(); for (int i = 0; i < n ; i++) { if (str2.indexOf(str1.substring(i, i + 1)) == -1) { System.out.println("false"); break; } if (i == n - 1) { System.out.println("true"); } } } }
我上一题做的是那个去重,所以我最开始用的Set集合,不重复集合去添加,最后==就是true,否则false;
直接比就行了,截取str1的每一个单个字符串,去找str2.indexof,==-1就是不存在就false,否则true,测试结果速度内存逗比用Set快