002 给一串string 比如“I have a card”再给一个键盘char[]。 键盘里有h,a,v,e。问句子里几个字可以被键盘打出来。只有have和a可以。 然后所有标点符号都可以被打出来

get the number of words where all of its characters are in the given keyboard array.

the naive way to check it is: break every word. and checking every char in each word. if every char is in, then we have a part of our solution.

HashSet<Character> set = new HashSet<>();
for (char c: keyboard) {
  set.add(c);
}
int res = 0;
String[] words = s.split(" "); //we need to pay attention of this, since there may exist other chars in that given string
for (String word: words) {
  int flag = true;
  int i = 0;
  while (i < word.length()) {
    if (!set.containsKey(word.charAt(i))) {
      flag = false;
      break;
    }
  }
  if (flag) {
    res++;
  }
}
return res;

standard answer:

public class brokeKeyboard {
    public int solution(String sentence, List<Character> list){
         if(list.size() == 0){
             return sentence.split(" ").length;
         }
         int res = 0;
         boolean flag = false;
         String[] words = sentence.toLowerCase().split(" ");//先化为小写,然后按空格分割
         for(String word: words){
             for(Character i: word.toCharArray()){
                 if(Character.isLowerCase(i)){
                     if(!list.contains(i)){//如果出现不存在的字母立马break,走完循环res就加一
                         flag = true;
                         break;
                     }
                 }
             }
             if(!flag) {
                 res++;
             }else{
                 flag = false;
             }
         }
         return res;
    }

    public static void main(String[] args){
        brokeKeyboard bk = new brokeKeyboard();
        String sentence = "hEllo##, This^^";
        List<Character> list = Arrays.asList('i','e','o','l','h');
        int res = bk.solution(sentence, list);
        System.out.print("res: ");
        System.out.print(res);
    }
}
posted @ 2020-09-28 04:15  EvanMeetTheWorld  阅读(24)  评论(0)    收藏  举报