哈希表
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
String str = br.readLine();
Set<Character> set = new HashSet<>(1 << 8);
for (char c : str.toCharArray()) {
set.add(c);
}
pw.println(set.size());
pw.flush();
pw.close();
br.close();
}
}
也可以用数组代替哈希表,效率可能会高一点点
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
String str = br.readLine();
boolean[] record = new boolean[1 << 7];
for (char c : str.toCharArray()) {
record[c] = true;
}
int ans = 0;
for (boolean r : record) {
ans += r ? 1 : 0;
}
pw.println(ans);
pw.flush();
pw.close();
br.close();
}
}

京公网安备 11010502036488号