目录
题目描述
思路
统计字符串不同字符的个数,其实就是对一个字符数组去重,可以使用Set集合
代码实现
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
// 使用HashSet来存储出现过的不同字符,自动去重
String s = sc.nextLine();
Set<Character> distinctChars = new HashSet<>();
for (char c : s.toCharArray()) {
// 只处理ASCII码在0~127范围内的字符
if (c >= 0 && c <= 127 && c != '\n') {
distinctChars.add(c);
}
}
System.out.print(distinctChars.size());
}
}