【PAT甲級 刪除字符串中重複字母】1084 Broken Keyboard (20 分) Java 全部AC

題目

在這裏插入圖片描述


題解 Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str1 = br.readLine();
		String str2 = br.readLine();

		// 刪除str1中與str2相同的部分
		for (int i = 0; i < str2.length(); i++) {
			str1 = str1.replaceAll(str2.charAt(i) + "", "");
		}
		str1=str1.toUpperCase();
		
		// 刪除str1本身重複的部分
		StringBuilder sb = new StringBuilder(str1);
		for (int i = 0; i < sb.length(); i++) {
			for (int j = i+1; j < sb.length(); j++) {
				if (sb.charAt(i) == sb.charAt(j)) {
					sb.deleteCharAt(j);
					j--;//重要
				}
			}
		}
		
		System.out.println(sb);
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章