import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String s = in.nextLine(); Set<Character> set = new HashSet<>(); for (char c : s.toCharArray()) { set.add(c); } System.out.println(set.size()); } }
可以直接用set遍历整个字符串,也可以用数组存储相应字符出现的次数,重复多次只算一个也能解决