Skip to content

Commit 26c4ae7

Browse files
committed
Runtime: 0 ms (Top 100.0%) | Memory: 41.60 MB (Top 23.65%)
1 parent 73ef687 commit 26c4ae7

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
// Runtime: 0 ms (Top 100.0%) | Memory: 41.60 MB (Top 23.65%)
2+
13
class Solution {
24
public char repeatedCharacter(String s) {
3-
HashSet<Character> hset = new HashSet<>();
4-
for(char ch:s.toCharArray())
5-
{
6-
if(hset.contains(ch))
7-
return ch;
8-
else
9-
hset.add(ch);
5+
HashSet<Character> set = new HashSet<>();//Create a set of characters
6+
for(int i = 0 ; i < s.length() ; i++){
7+
if(set.contains(s.charAt(i))) return s.charAt(i);//If the set already contains the current character, then it is the required ans
8+
set.add(s.charAt(i));
109
}
11-
return ' ';
10+
return 'a';//As it is given in the question that there is at least one letter that appears twice, therefore it is certain that the ans will be found before we reach this statement. So, just adding any random return statement so that there is no error in the code.
1211
}
13-
}
12+
}

0 commit comments

Comments
 (0)