Skip to content

Commit

Permalink
Added Java solution for "First Letter to Appear Twice"
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrylo-Ktl committed Oct 27, 2022
1 parent a5578fa commit 8aa56b3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Java/First Letter to Appear Twice.java
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 modified infrastructure/src/resources/problems.db
Binary file not shown.

0 comments on commit 8aa56b3

Please sign in to comment.