forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added FirstUniqueCharacterInAString.java
- Loading branch information
Kevin Naughton Jr
authored and
Kevin Naughton Jr
committed
Jun 4, 2018
1 parent
a904bdf
commit 2dd7ba0
Showing
6 changed files
with
204 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,34 @@ | ||
//Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. | ||
// | ||
//Examples: | ||
// | ||
//s = "leetcode" | ||
//return 0. | ||
// | ||
//s = "loveleetcode", | ||
//return 2. | ||
//Note: You may assume the string contain only lowercase letters. | ||
|
||
class FirstUniqueCharacterInAString { | ||
public int firstUniqChar(String s) { | ||
HashMap<Character, Integer> characters = new HashMap<Character, Integer>(); | ||
for(int i = 0; i < s.length(); i++) { | ||
char current = s.charAt(i); | ||
if(characters.containsKey(current)) { | ||
characters.put(current, -1); | ||
} else { | ||
characters.put(current, i); | ||
} | ||
} | ||
|
||
int min = Integer.MAX_VALUE; | ||
for(char c: characters.keySet()) { | ||
if(characters.get(c) > -1 && characters.get(c) < min) { | ||
min = characters.get(c); | ||
} | ||
} | ||
|
||
return min == Integer.MAX_VALUE ? -1 : min; | ||
|
||
} | ||
} |
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,34 @@ | ||
//Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. | ||
// | ||
//Examples: | ||
// | ||
//s = "leetcode" | ||
//return 0. | ||
// | ||
//s = "loveleetcode", | ||
//return 2. | ||
//Note: You may assume the string contain only lowercase letters. | ||
|
||
class FirstUniqueCharacterInAString { | ||
public int firstUniqChar(String s) { | ||
HashMap<Character, Integer> characters = new HashMap<Character, Integer>(); | ||
for(int i = 0; i < s.length(); i++) { | ||
char current = s.charAt(i); | ||
if(characters.containsKey(current)) { | ||
characters.put(current, -1); | ||
} else { | ||
characters.put(current, i); | ||
} | ||
} | ||
|
||
int min = Integer.MAX_VALUE; | ||
for(char c: characters.keySet()) { | ||
if(characters.get(c) > -1 && characters.get(c) < min) { | ||
min = characters.get(c); | ||
} | ||
} | ||
|
||
return min == Integer.MAX_VALUE ? -1 : min; | ||
|
||
} | ||
} |
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,34 @@ | ||
//Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. | ||
// | ||
//Examples: | ||
// | ||
//s = "leetcode" | ||
//return 0. | ||
// | ||
//s = "loveleetcode", | ||
//return 2. | ||
//Note: You may assume the string contain only lowercase letters. | ||
|
||
class FirstUniqueCharacterInAString { | ||
public int firstUniqChar(String s) { | ||
HashMap<Character, Integer> characters = new HashMap<Character, Integer>(); | ||
for(int i = 0; i < s.length(); i++) { | ||
char current = s.charAt(i); | ||
if(characters.containsKey(current)) { | ||
characters.put(current, -1); | ||
} else { | ||
characters.put(current, i); | ||
} | ||
} | ||
|
||
int min = Integer.MAX_VALUE; | ||
for(char c: characters.keySet()) { | ||
if(characters.get(c) > -1 && characters.get(c) < min) { | ||
min = characters.get(c); | ||
} | ||
} | ||
|
||
return min == Integer.MAX_VALUE ? -1 : min; | ||
|
||
} | ||
} |
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,34 @@ | ||
//Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. | ||
// | ||
//Examples: | ||
// | ||
//s = "leetcode" | ||
//return 0. | ||
// | ||
//s = "loveleetcode", | ||
//return 2. | ||
//Note: You may assume the string contain only lowercase letters. | ||
|
||
class FirstUniqueCharacterInAString { | ||
public int firstUniqChar(String s) { | ||
HashMap<Character, Integer> characters = new HashMap<Character, Integer>(); | ||
for(int i = 0; i < s.length(); i++) { | ||
char current = s.charAt(i); | ||
if(characters.containsKey(current)) { | ||
characters.put(current, -1); | ||
} else { | ||
characters.put(current, i); | ||
} | ||
} | ||
|
||
int min = Integer.MAX_VALUE; | ||
for(char c: characters.keySet()) { | ||
if(characters.get(c) > -1 && characters.get(c) < min) { | ||
min = characters.get(c); | ||
} | ||
} | ||
|
||
return min == Integer.MAX_VALUE ? -1 : min; | ||
|
||
} | ||
} |
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,34 @@ | ||
//Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. | ||
// | ||
//Examples: | ||
// | ||
//s = "leetcode" | ||
//return 0. | ||
// | ||
//s = "loveleetcode", | ||
//return 2. | ||
//Note: You may assume the string contain only lowercase letters. | ||
|
||
class FirstUniqueCharacterInAString { | ||
public int firstUniqChar(String s) { | ||
HashMap<Character, Integer> characters = new HashMap<Character, Integer>(); | ||
for(int i = 0; i < s.length(); i++) { | ||
char current = s.charAt(i); | ||
if(characters.containsKey(current)) { | ||
characters.put(current, -1); | ||
} else { | ||
characters.put(current, i); | ||
} | ||
} | ||
|
||
int min = Integer.MAX_VALUE; | ||
for(char c: characters.keySet()) { | ||
if(characters.get(c) > -1 && characters.get(c) < min) { | ||
min = characters.get(c); | ||
} | ||
} | ||
|
||
return min == Integer.MAX_VALUE ? -1 : min; | ||
|
||
} | ||
} |
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,34 @@ | ||
//Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. | ||
// | ||
//Examples: | ||
// | ||
//s = "leetcode" | ||
//return 0. | ||
// | ||
//s = "loveleetcode", | ||
//return 2. | ||
//Note: You may assume the string contain only lowercase letters. | ||
|
||
class FirstUniqueCharacterInAString { | ||
public int firstUniqChar(String s) { | ||
HashMap<Character, Integer> characters = new HashMap<Character, Integer>(); | ||
for(int i = 0; i < s.length(); i++) { | ||
char current = s.charAt(i); | ||
if(characters.containsKey(current)) { | ||
characters.put(current, -1); | ||
} else { | ||
characters.put(current, i); | ||
} | ||
} | ||
|
||
int min = Integer.MAX_VALUE; | ||
for(char c: characters.keySet()) { | ||
if(characters.get(c) > -1 && characters.get(c) < min) { | ||
min = characters.get(c); | ||
} | ||
} | ||
|
||
return min == Integer.MAX_VALUE ? -1 : min; | ||
|
||
} | ||
} |