Skip to content

Commit

Permalink
added FirstUniqueCharacterInAString.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Naughton Jr authored and Kevin Naughton Jr committed Jun 4, 2018
1 parent a904bdf commit 2dd7ba0
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 0 deletions.
34 changes: 34 additions & 0 deletions company/amazon/FirstUniqueCharacterInAString.java
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;

}
}
34 changes: 34 additions & 0 deletions company/bloomberg/FirstUniqueCharacterInAString.java
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;

}
}
34 changes: 34 additions & 0 deletions company/google/FirstUniqueCharacterInAString.java
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;

}
}
34 changes: 34 additions & 0 deletions company/microsoft/FirstUniqueCharacterInAString.java
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;

}
}
34 changes: 34 additions & 0 deletions leetcode/hash-table/FirstUniqueCharacterInAString.java
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;

}
}
34 changes: 34 additions & 0 deletions leetcode/string/FirstUniqueCharacterInAString.java
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;

}
}

0 comments on commit 2dd7ba0

Please sign in to comment.