Java Coding 14 - 找出字符串中缺失的英文字母

需求:
找出字符串中缺失的英文字母。
例如:I love testing
缺失的英文字母:
A B C D E F G H J K L M N O P Q R S T U V W X Y Z a b c d f h j k m p q r u w x y z

思路:

  1. 利用HashMap,将字母表A-Z, a-z作为key,初始出现的次数0存入HashMap中,遍历字符串,将字符串出现的字符value 加 1.最后输出HashMap中value为0的字母就是缺失的字母。
  2. 利用HashSetremoveAll方法,分别将字母表中的字母和字符串存到两个HashSet中,removeAll移除相同的字母,剩下的就是缺失的字母。

实现:
HashMap

for(char c = 'A'; c <= 'z'; c++){
            System.out.print(c);
 }

输出:中间多出了几个非英文字母

ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz
public static void printMissingAlphabetsInStringUsingHashMap(String str){
        Map<Character, Integer> alphabets = new HashMap<>();
        for(char c = 'A'; c <= 'Z'; c++){
            alphabets.put(c, 0);
        }
        for(char c = 'a'; c <= 'z'; c++){
            alphabets.put(c, 0);
        }
        for(int i = 0; i < str.length(); i++){
            char c = str.charAt(i);
            // ignore blank space
            // or if(c != ' ')
            if(alphabets.containsKey(c)){
                alphabets.put(c, alphabets.get(c)+1);
            }
        }
        for(Map.Entry<Character, Integer> entry : alphabets.entrySet()){
            if(entry.getValue() == 0){
                System.out.print(entry.getKey() + " ");
            }
        }
        System.out.println();
    }

HashSet:
split("")将字符串每个字符分开

public static void printMissingAlphabetsInStringUsingHashSet(String str){
        str = str.replace(" ", "");
        String[] strArray = str.split("");
        String[] alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLNMOPQRSTUVWXYZ".split("");

        Set<String> strSet = new HashSet<>(Arrays.asList(strArray));
        Set<String> alphabetsSet = new HashSet<>(Arrays.asList(alphabets));

        alphabetsSet.removeAll(strSet);

        System.out.println(alphabetsSet);

    }

完整代码:

import java.util.*;

public class Test {

    public static void main(String[] args) {
        testFindMissingAlphabetsInString();
    }
    public static void testFindMissingAlphabetsInString(){
        System.out.println("please enter a string to find the missing alphabets, input quit means to exit");
        Scanner in = new Scanner(System.in);
        String str;
        str = in.nextLine();
        while(!str.equals("quit")){
            System.out.println("the missing alphabets is:");
            printMissingAlphabetsInStringUsingHashMap(str);
            //printMissingAlphabetsInStringUsingHashSet(str);            
            System.out.println("please enter a string a again, input quit means to exit");
            str = in.nextLine();
        }
    }

    public static void printMissingAlphabetsInStringUsingHashMap(String str){
        Map<Character, Integer> alphabets = new HashMap<>();
        for(char c = 'A'; c <= 'Z'; c++){
            alphabets.put(c, 0);
        }
        for(char c = 'a'; c <= 'z'; c++){
            alphabets.put(c, 0);
        }
        for(int i = 0; i < str.length(); i++){
            char c = str.charAt(i);
            // ignore blank space
            // or if(c != ' ')
            if(alphabets.containsKey(c)){
                alphabets.put(c, alphabets.get(c)+1);
            }
        }
        for(Map.Entry<Character, Integer> entry : alphabets.entrySet()){
            if(entry.getValue() == 0){
                System.out.print(entry.getKey() + " ");
            }
        }
        System.out.println();
    }

    public static void printMissingAlphabetsInStringUsingHashSet(String str){
        str = str.replace(" ", "");
        String[] strArray = str.split("");
        String[] alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLNMOPQRSTUVWXYZ".split("");

        Set<String> strSet = new HashSet<>(Arrays.asList(strArray));
        Set<String> alphabetsSet = new HashSet<>(Arrays.asList(alphabets));

        alphabetsSet.removeAll(strSet);

        System.out.println(alphabetsSet);

    }

}

结果:

please enter a string to find the missing alphabets, input quit means to exit
I love Testing
the missing alphabets is:
A B C D E F G H J K L M N O P Q R S U V W X Y Z a b c d f h j k m p q r u w x y z 
please enter a string a again, input quit means to exit
I LOVE TESTING
the missing alphabets is:
A B C D F H J K M P Q R U W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 
please enter a string a again, input quit means to exit
quit

Process finished with exit code 0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值