LeetCode 387. First Unique Character in a String

简介: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。

v2-92f5311f7b788ec76764bcc4dd791008_1440w.jpg

Description



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.


描述



给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。


案例:


s = "leetcode"
返回 0.
s = "loveleetcode",
返回 2.


来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/first-unique-character-in-a-string

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


思路


  1. 看到这道题的第一个思路是利用字典,首先统计字符串 s 中每个字符出现的次数,然后顺序遍历 s,返回第一个计数为 1 的字符,但是这样提交后发现时间消耗很大。代码如下:


from collections import Counter
class Solution:
    def firstUniqChar(self, s: str) -> int:
        res = -1
        count = Counter(s)
        for i,c in enumerate(s):
            if count[c]==1:
                res = i
                break
        return res
  1. 另外一种高效的方法是:由于字母一共只有 26 个,所以最后的结果一定是这 26 个字母中的一个;于是我们依次检查这个 26 个字母在字符串中出现的次数,返回只出现了一次,并且顺序第一的字母的索引。


class Solution:
    def firstUniqChar(self, s: str) -> int:
        res = len(s)
        for char in 'abcdefghijklmnopqrstuvwxyz':
            # 从左右两边找 char 出现的位置,如果 char 在 s 中仅仅出现一次;
            # 那么 left 和 right 一定相等;
            left, right = s.find(char), s.rfind(char)
            if left != -1 and left == right:
                res = min(res, left)
        return -1 if res == len(s) else res

源代码文件在 这里


目录
相关文章
|
6月前
|
算法 C++
【LeetCode】【C++】string OJ必刷题
【LeetCode】【C++】string OJ必刷题
36 0
|
1月前
|
机器学习/深度学习 canal NoSQL
从C语言到C++_12(string相关OJ题)(leetcode力扣)
从C语言到C++_12(string相关OJ题)(leetcode力扣)
31 0
|
1月前
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
7月前
|
Java
Leetcode 467. Unique Substrings in Wraparound String
大概翻译下题意,有个无限长的字符串s,是由无数个「abcdefghijklmnopqrstuvwxy」组成的。现在给你一个字符串p,求多少个p的非重复子串在s中出现了?
29 0
|
算法 索引
【LeetCode】string 类的几道简单题
【LeetCode】string 类的几道简单题
【LeetCode】string 类的几道简单题
LeetCode 438. Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter.
67 0
LeetCode 438. Find All Anagrams in a String
LeetCode 424. Longest Repeating Character Replacem
给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次。在执行上述操作后,找到包含重复字母的最长子串的长度。
79 0
LeetCode 424. Longest Repeating Character Replacem
|
存储
LeetCode 394. Decode String
给定一个经过编码的字符串,返回它解码后的字符串。 编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。 你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。 此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k ,例如不会出现像 3a 或 2[4] 的输入。
68 0
LeetCode 394. Decode String
|
存储 C语言 C++
Leetcode17. 电话号码的字母组合:递归树深度遍历(C++vector和string的小练习)
Leetcode17. 电话号码的字母组合:递归树深度遍历(C++vector和string的小练习)
|
存储 canal 算法
leetcode:43. 字符串相乘(附加一些C++string其他小练习)
leetcode:43. 字符串相乘(附加一些C++string其他小练习)