LeetCode-387. 字符串中的第一个唯一字符【队列 哈希表 字符串 计数】

题目描述:

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

示例 1:
输入: s = “leetcode”
输出: 0

示例 2:
输入: s = “loveleetcode”
输出: 2

示例 3:
输入: s = “aabb”
输出: -1

提示:

1 <= s.length <= 105
s 只包含小写字母

解题思路一:用一个哈希表记录所有字符出现的次数,用一个列表unique_chars 记录出现一次的字符,然后从头遍历s,判断当前字符是否位于unique_chars中即可得出答案。

class Solution:
    def firstUniqChar(self, s: str) -> int:
        dic = {
   }

        # 记录字符出现次数
        for c in s:
            dic[c] = dic[c] + 1 if c in dic else 1

        # 过滤出现次数不为一的字符
        unique_chars = [k for k, v in filter(lambda kvp: kvp[1] == 1, dic.items())]
        # 遍历目标字符串,返回首个出现在unique_chars中的字符的索引
        for i, c in enumerate(s):
            if c in unique_chars:
                return i
        
        return -1

更简洁的写法

class Solution:
    def firstUniqChar(self, s: str) -> int:
        frequency = collections.Counter(s)
        for i, ch in enumerate(s):
            if frequency[ch] == 1:
                return i
        return -1

时间复杂度:O(n)
空间复杂度:O(26)

解题思路二:使用哈希表存储索引,出现多次变为-1,我们只需要找到哈希表中值不为-1的最小地址即可。

class Solution:
    def firstUniqChar(self, s: str) -> int:
        position = {
   }
        n = len(s)
        for i, ch in enumerate(s):
            if ch in position:
                position[ch] = -1
            else:
                position[ch] = i
        first = n
        for pos in position.values():
            if pos != -1 and pos < first:
                first = pos
        if first == n:
            first = -1
        return first

时间复杂度:O(n)
空间复杂度:O(26)

解题思路三:队列,用哈希表记录元素出现的位置,重复出现则标记为-1。注意这里入队的是字典元素(s[i], i)

class Solution:
    def firstUniqChar(self, s: str) -> int:
        position = dict()
        q = collections.deque()
        n = len(s)
        for i, ch in enumerate(s):
            if ch not in position:
                position[ch] = i
                q.append((s[i], i))
            else:
                position[ch] = -1
                while q and position[q[0][0]] == -1:
                    q.popleft()
        return -1 if not q else q[0][1]

时间复杂度:O(n)
空间复杂度:O(26)

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2023-12-15 10:14:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-15 10:14:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-15 10:14:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-15 10:14:05       20 阅读

热门阅读

  1. Windows进程机制

    2023-12-15 10:14:05       32 阅读
  2. LeetCode 2415. 反转二叉树的奇数层

    2023-12-15 10:14:05       40 阅读
  3. 详解WebMvcConfigurer用法

    2023-12-15 10:14:05       28 阅读
  4. 学通python

    2023-12-15 10:14:05       35 阅读
  5. 决算报表软件---政府财政管理系统

    2023-12-15 10:14:05       25 阅读
  6. 源码赏析: 数据结构转换工具 configor (一)

    2023-12-15 10:14:05       35 阅读
  7. ElasticSearch之cat shards API

    2023-12-15 10:14:05       33 阅读
  8. LeetCode 14 最长公共前缀

    2023-12-15 10:14:05       48 阅读
  9. 相机的CL、USB3.0、1394、USB2.0和GIGE接口详解和区别

    2023-12-15 10:14:05       75 阅读
  10. Springboot自带logback日志配置学习

    2023-12-15 10:14:05       49 阅读