public class Solution {
public int FirstUniqChar(string s) {
int min = Int32.MaxValue;
for (char c = 'a'; c <= 'z'; c++)
{
int tmp = s.IndexOf(c);
if(tmp!=-1 && tmp == s.LastIndexOf(c))
{
min = tmp<min ? tmp : min;
}
}
min = min == Int32.MaxValue ? -1 : min;
return min;
}
}
In the second solution, we enumerate the alphabet ( 26 times instead of solution1’s length of s times)
If IndexOf equals as LastIndexOf means that there are only one character in the word s.
Runtime : 96ms、79ms、83ms ≈ Faster than 90% ⬆️
Time Complexity : O(n)
Java Solution
Solution1
class Solution {
public int firstUniqChar(String s) {
Map<Character, Integer> dict = new HashMap<Character, Integer>();
HashSet<Character> hSet = new HashSet<Character>();
for(int i = 0; i<s.length();i++)
{
char c = s.charAt(i);
if(hSet.contains(c))
{
continue;
}
if (!dict.containsKey(c))
{
dict.put(c, i);
}
else
{
dict.remove(c);
hSet.add(c);
}
}
if(dict.size() == 0)
{
return -1;
}
int min = Integer.MAX_VALUE;
for (Integer value : dict.values()) {
min = value<min ? value : min;
}
return min;
}
}
Runtime : 21ms、16ms、17ms ≈ Faster than 70%
⭐Solution2
class Solution {
public int firstUniqChar(String s) {
int min = Integer.MAX_VALUE;
for (char c = 'a'; c <= 'z'; c++)
{
int tmp = s.indexOf(c);
if(tmp!=-1 && tmp == s.lastIndexOf(c))
{
min = tmp<min ? tmp : min;
}
}
min = min == Integer.MAX_VALUE ? -1 : min;
return min;
}
}
Runtime : 2ms、2ms、1ms ≈ Faster than 99.66~100%
Python3 Solution
⭐Solution1
from string import ascii_lowercase as alc
class Solution:
def firstUniqChar(self, s: str) -> int:
m = math.pow(10,5); #min
for c in alc:
try:
tmp = s.index(c)
tmp2 = s.rindex(c)
except:
continue;
if tmp == tmp2:
m = tmp if tmp<m else m
return -1 if m == math.pow(10,5) else m
Runtime : 39ms、15ms、35ms ≈ Faster than 99.99~100%
JavaScript Solution
⭐Solution1
/**
* @param {string} s
* @return {number}
*/
var firstUniqChar = function(s) {
var min = 10**5;
for (var i = 97; i <= 122; i++) {
var c = String.fromCharCode(i);
var tmp = s.indexOf(c);
if(tmp!=-1 && tmp == s.lastIndexOf(c))
{
min = tmp<min ? tmp : min;
}
}
min = min == 10**5 ? -1 : min;
return min;
};
🧡You can support me by clicking some ad, Thanks a lot
✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know (either leave a comment or contact me by Messenger is ok!)
ai_front = {"insertion_before":"BEFORE","insertion_after":"AFTER","insertion_prepend":"PREPEND CONTENT","insertion_append":"APPEND CONTENT","insertion_replace_content":"REPLACE CONTENT","insertion_replace_element":"REPLACE ELEMENT","visible":"VISIBLE","hidden":"HIDDEN","fallback":"FALLBACK","automatically_placed":"Automatically placed by AdSense Auto ads code","cancel":"Cancel","use":"Use","add":"Add","parent":"Parent","cancel_element_selection":"Cancel element selection","select_parent_element":"Select parent element","css_selector":"CSS selector","use_current_selector":"Use current selector","element":"ELEMENT","path":"PATH","selector":"SELECTOR"};