好未来:删除公共字符串

题目描述

输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”
输入描述:
每个测试输入包含2个字符串
输出描述:
输出删除后的字符串
示例1
输入

They are students.
aeiou
输出

Thy r stdnts.

思路:

我SB似的用HashSet,然后发现了更牛X的方法

HashSet:

import java.util.Scanner;
import java.util.HashSet;

public class Main

public static String solution(String s1, String s2) {
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
HashSet<Character> set = new HashSet<>();
for(int i = 0; i < c2.length; i++) {
set.add(c2[i]);
}
StringBuilder sb = new StringBuilder();
for(int i = 0; i < c1.length; i++) {
if(!set.contains(c1[i])) {
sb.append(c1[i]);
}
}
return sb.toString();
}

public static void main(String[] args) {
Scanner sc = new

正则:

链接:https://www.nowcoder.com/questionTerminal/f0db4c36573d459cae44ac90b90c6212
来源:牛客网

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s1 = sc.nextLine();
String s2 = sc.nextLine();
String pattern = "[" + s2 + "]";
String result = s1.replaceAll(pattern, "");
System.out.println(result);
}
}
}