文字列を考えると、その最初の非反復文字を見つけると、そのインデックスを返します。ない場合は、-1を返します。
ケース:
S = "leetcode"
リターン0。
S = "loveleetcode"、
2に戻ります。
注:文字列が小文字のみが含まれていることを前提とすることができます。
ソリューション:
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
}