需求:
找出字符串中缺失的英文字母。
例如: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
思路:
- 利用HashMap,将字母表A-Z, a-z作为key,初始出现的次数0存入HashMap中,遍历字符串,将字符串出现的字符value 加 1.最后输出HashMap中value为0的字母就是缺失的字母。
- 利用HashSet中removeAll方法,分别将字母表中的字母和字符串存到两个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