3170. 删除星号以后字典序最小的字符串

题目链接 3170. 删除星号以后字典序最小的字符串
思路 堆栈 & 位运算
题解链接 三种写法:26 个栈+位运算优化(Python/Java/C++/Go)
关键点 1. 用堆栈跟踪各个字母出现的位置 2. 用位运算跟踪当前最小字母(lowbit技巧
时间复杂度 朴素做法:O(n|Σ|) 位运算优化:O(n+|Σ|)
空间复杂度 朴素做法:O(n+|Σ|) 位运算优化:O(n+|Σ|)

代码实现(朴素做法):

class Solution:
def clearStars(self, s: str) -> str:
s = list(s)
stack = [[] for _ in range(26)]
for i, ch in enumerate(s):
if ch != '*':
stack[ord(ch) - ord('a')].append(i)
continue
for positions in stack:
if positions:
s[positions.pop()] = '*'
break
return "".join(ch for ch in s if ch != "*")

代码实现(位运算优化):

class Solution:
def clearStars(self, s: str) -> str:
s = list(s)
stack = [[] for _ in range(26)]
mask = 0
for i, ch in enumerate(s):
if ch != '*':
ch = ord(ch) - ord('a')
stack[ch].append(i)
mask |= 1 << ch
else:
lowbit = mask & -mask
position = stack[lowbit.bit_length() - 1]
s[position.pop()] = '*'
if not position:
mask ^= lowbit
return "".join(ch for ch in s if ch != '*')
posted @   WrRan  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
点击右上角即可分享
微信分享提示