方法:计数
class Solution {
public char repeatedCharacter(String s) {
int[] counter = new int[26];
for (char ch : s.toCharArray()) {
counter[ch - 'a']++;
if (counter[ch - 'a'] > 1) {
return ch;
}
}
return 'a';
}
}
方法二:哈希表
class Solution {
public char repeatedCharacter(String s) {
Set<Character> set = new HashSet<>();
for (char ch : s.toCharArray()) {
if (!set.add(ch)) {
return ch;
}
}
return 'a';
}
}