当前位置: 首页 > article >正文

Leetcode 387. First Unique Character in a String

Problem

Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

Algorithm

Use two lists: one list is used to count the letters in “s”; the other list is the position where the letter first appears. Then find the smallest position of the letters appear once.

Code

class Solution:
    def firstUniqChar(self, s: str) -> int:
        sCnts = [0] * 26
        sStart = [0] * 26
        cnts = 0
        for c in s:
            sCnts[ord(c) - ord('a')] += 1
            if sCnts[ord(c) - ord('a')] == 1:
                sStart[ord(c) - ord('a')] = cnts
            cnts += 1
        
        index = -1
        for i in range(26):
            if sCnts[i] == 1 and (index < 0 or index > sStart[i]):
                index = sStart[i]
        
        return index

http://www.kler.cn/news/274217.html

相关文章:

  • 【研发日记】Matlab/Simulink技能解锁(五)——Simulink布线技巧
  • 在sql server 2016 always on集群里新增一个数据库节点
  • 【数据结构】链表力扣刷题详解
  • Flutter 初始WidgetState 简单应用案例分析
  • LAMP架构部署--yum安装方式
  • 微信小程序小白易入门基础教程1
  • pytorch DDP模式下, 获取数据的的preftech + stream
  • 【SVG】前端-不依靠第三方包怎么画连线???
  • 装X神器,装X图片生成器,高富帅模拟器
  • 安卓基础面试题
  • 02_Linux文件权限和目录配置
  • 论文阅读——MoCo
  • 014 Linux_同步
  • Linux下使用ntpdate进行时间同步
  • 8.python中的元组
  • Java13_反转字符串中的单词 III(方法二String转换成字符数组)
  • Java的图书管理系统,确实有两把斧子 ! ! !
  • 网络——入门基础
  • ES代替品:轻量级搜索引擎MeiliSearch
  • HarmonyOS NEXT应用开发—投票动效实现案例