Given a string, find its first non-repeating characters, and returns its index. If not, it returns -1.
Case:
s = "leetcode"
return 0.
s = "loveleetcode",
return to 2.
Note: You can assume that the string contains only lowercase letters.
solution:
func firstUniqChar(_ s: String) -> Int {
for item in s {
if let index = s.firstIndex(of: item) {
if index == s.lastIndex(of: item) {
return index.encodedOffset
}
}
}
return -1
}