-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Java solution for "First Letter to Appear Twice"
- Loading branch information
1 parent
a5578fa
commit 8aa56b3
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class Solution { | ||
|
||
/** | ||
* Time: O(n) | ||
* Memory: O(1) | ||
*/ | ||
public char repeatedCharacter(String s) { | ||
Set<Character> seen = new HashSet<>(); | ||
|
||
for (char c : s.toCharArray()) { | ||
if (seen.contains(c)) | ||
return c; | ||
seen.add(c); | ||
} | ||
|
||
return '#'; | ||
} | ||
} | ||
|
||
|
||
public class Solution { | ||
|
||
/** | ||
* Time: O(n) | ||
* Memory: O(1) | ||
*/ | ||
public char repeatedCharacter(String s) { | ||
boolean[] seen = new boolean[26]; | ||
|
||
for (char c : s.toCharArray()) { | ||
if (seen[c - 'a']) | ||
return c; | ||
seen[c - 'a'] = true; | ||
} | ||
|
||
return '#'; | ||
} | ||
} |
Binary file not shown.