牛客笔试题---BrokenKeyboard

这道题目描述了一个破损键盘的问题,其中某些键在输入时不会显示。给定原始字符串和实际输入的字符串,任务是找出那些确定磨损的键。输入包含一个测试用例,每个案例中,第一行是原始字符串,第二行是实际输入的字符串,两者长度不超过80个字符,包括大小写字母、数字和下划线。确保两个字符串都不为空,至少有一个键是磨损的。输出需要按检测到的顺序列出磨损的键,每个键只打印一次,且字母需大写。
摘要由CSDN通过智能技术生成

题目描述:

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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值