Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
Example:
Input: s = "leetcode"
Output: 0Example:
Input: s = "loveleetcode"
Output: 2Example:
Input: s = "aabb"
Output: -1這題主要是找第一個唯一的char, 因此for loop並且比對index即可。
- java
class Solution {
public int firstUniqChar(String s) {
Integer index = null;
Integer lastIndex = null;
for(char c : s.toCharArray()){
index = s.indexOf(c);
lastIndex = s.lastIndexOf(c);
if(index.equals(lastIndex)) {
return index;
}
}
return -1;
}
}- python
class Solution: