首页 > 试题广场 >

删除公共字符

[编程题]删除公共字符
  • 热度指数:738 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。
例如:第一个字符串是"They are students.",第二个字符串是”aeiou"。删除之后的第一个字符串变成"Thy r stdnts."。
保证两个字符串的长度均不超过100。

输入描述:
输入两行,每行一个字符串。


输出描述:
输出删除后的字符串。
示例1

输入

They are students. 
aeiou

输出

Thy r stdnts.
import java.util.Scanner;
import java.util.HashSet;
import java.util.Set;

// 注意类名必须为 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 a = in.nextLine();
            String b = in.nextLine();
            String c=delete(a,b);
            System.out.println(c);
        }
    }
    public static String delete(String str1,String str2){
        Set<Character> set = new HashSet<>();
        for(int i=0;i<str2.length();i++){
            set.add(str2.charAt(i));
        }
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<str1.length();i++){
            char ch = str1.charAt(i);
            if (!set.contains(ch)) {
                sb.append(ch);
            }
        }
        return sb.toString();
    }
}
发表于 2023-04-21 14:48:06 回复(0)

问题信息

难度:
1条回答 2105浏览

热门推荐

通过挑战的用户

删除公共字符