[LeetCode] First Letter to Appear Twice

2351. First Letter to Appear Twice

Given a string s consisting of lowercase English letters, return the first letter to appear twice.

Note:

  • A letter a appears twice before another letter b if the second occurrence of a is before the second occurrence of b.
  • s will contain at least one letter that appears twice.
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
char repeatedCharacter(string s) {
unordered_set<char> us;
for(auto& ch : s) {
if(us.count(ch)) return ch;
us.insert(ch);
}
return '#';
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/24/PS/LeetCode/first-letter-to-appear-twice/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.