题目描述:
On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen.Now given a string that you are supposed to type, and the string that you actually type out, please list those keys which are for sure worn out.
输入描述:
Each input file contains one test case. For each case, the 1st line contains the original string, and the 2nd line contains the typed-out string. Each string contains no more than 80 characters which are either English letters [A-Z] (case insensitive), digital numbers [0-9], or "_" (representing the space). It is guaranteed that both strings are nonempty.
输出描述:
For each test case, print in one line the keys that are worn out, in the order of being detected. The English letters must be capitalized.
Each worn out key must be printed once only. It is guaranteed that there is at least one worn out key.
示例1:
输入
7_This_is_a_test
_hs_s_a_es
输出
7TI
思路:broken keyboard坏掉的键盘,有些键坏了当你打字时不会显示到屏幕上,现给出了一个字符串,让你找到其中磨损的是哪些键
package Test;
import java.util.ArrayList;
import java.util.Scanner;
/**
* package:Test
* Description:TODO
* @date:2019/7/13
* @Author:weiwei
**/
public class Brokenkeyboard {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String originalString = sc.next();
String typedOutString = sc.next();
ArrayList<Character> wornOutkeys = new ArrayList<>();
int size = originalString.length();
int iOriginal = 0;
int iTypeOut = 0;
while(iOriginal < size){
boolean wornOut = false; //假设没有损坏
char originalCh = originalString.charAt(iOriginal);
char originalUpper = Character.toUpperCase(originalCh);//全部大写
if(iTypeOut >= typedOutString.length()){
//输出的字符已经结束
wornOut = true;
}else {
char typeOutCh = typedOutString.charAt(iTypeOut);
char typeOutUpper = Character.toUpperCase(typeOutCh);
if(originalCh != typeOutUpper){
//应该看到输出的字符没有输出
wornOut = true;
}
}
if(wornOut){
if(!wornOutkeys.contains(originalUpper)){
wornOutkeys.add(originalUpper);
}
iOriginal++;
}else{
iOriginal++;
iTypeOut++;
}
}
for(int i = 0;i<wornOutkeys.size();i++){
System.out.print(wornOutkeys.get(i));
}
System.out.println();
}
}