From cd0bb8d030f59c278bfea80f585366626f964c72 Mon Sep 17 00:00:00 2001 From: bumpsoo Date: Sat, 7 Jun 2025 05:12:50 +0000 Subject: [PATCH] https://leetcode.com/problems/lexicographically-minimum-string-after-removing-stars --- ...cally_minimum_string_after_removing_stars.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 medium/lexicographically_minimum_string_after_removing_stars.py diff --git a/medium/lexicographically_minimum_string_after_removing_stars.py b/medium/lexicographically_minimum_string_after_removing_stars.py new file mode 100644 index 0000000..7dd490f --- /dev/null +++ b/medium/lexicographically_minimum_string_after_removing_stars.py @@ -0,0 +1,17 @@ +# https://leetcode.com/problems/lexicographically-minimum-string-after-removing-stars + +import heapq + +class Solution: + def clearStars(self, s: str) -> str: + result = list(s) + h = [] + for i in range(len(s)): + if s[i] == '*': + (_, _, origin) = heapq.heappop(h) + result[origin] = '' + result[i] = '' + else: + heapq.heappush(h, (s[i], -i, i)) + return ''.join(result) +