Skip to content

Commit

Permalink
feat: LC-0387: First Unique Character in a String
Browse files Browse the repository at this point in the history
add java solution for leetcode problem 387 - First Unique Character in a String
  • Loading branch information
galaumang committed Oct 12, 2023
1 parent 57c1e66 commit 4c3d16c
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dsalgo.string;

public class LC0387_FirstUniqueCharacter {
public int firstUniqChar(String s) {
int[] hash = new int[26];
int n = s.length();
for (int i = 0; i < n; i++) {
hash[s.charAt(i) - 'a']++;
}
for (int i = 0; i < n; i++) {
if (hash[s.charAt(i) - 'a'] == 1)
return i;
}
return -1;
}
}

0 comments on commit 4c3d16c

Please sign in to comment.