Skip to content

Commit

Permalink
feat: add remove-duplicate-letters
Browse files Browse the repository at this point in the history
Change-Id: Ia235c9510710931287527ccd0ee3b4bec3ea7ec5
  • Loading branch information
franklingu committed Oct 11, 2020
1 parent 31fbb53 commit 68275c6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ LeetCode is a very good website to sharpen your programming/problem-solving skil
|300|[Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/)|[Java](./algorithms/longest-increasing-subsequence/)|![Medium](https://img.shields.io/badge/-Medium-orange)|
|303|[Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/)|[Java](./algorithms/range-sum-query-immutable/)|![Easy](https://img.shields.io/badge/-Easy-green)|
|304|[Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/)|[Java](./algorithms/range-sum-query-2d-immutable/)|![Medium](https://img.shields.io/badge/-Medium-orange)|
|316|[Remove Duplicate Letters](https://leetcode.com/problems/remove-duplicate-letters/)|[Python](./algorithms/remove-duplicate-letters/)|![Medium](https://img.shields.io/badge/-Medium-orange)|
|317|[Shortest Distance from All Buildings](https://leetcode.com/problems/shortest-distance-from-all-buildings/)|[Python](./algorithms/shortest-distance-from-all-buildings/)|![Hard](https://img.shields.io/badge/-Hard-red)|
|328|[Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/)|[Java](./algorithms/odd-even-linked-list/)|![Medium](https://img.shields.io/badge/-Medium-orange)|
|330|[Patching Array](https://leetcode.com/problems/patching-array/)|[Java](./algorithms/patching-array/)|![Hard](https://img.shields.io/badge/-Hard-red)|
Expand Down Expand Up @@ -332,6 +333,7 @@ LeetCode is a very good website to sharpen your programming/problem-solving skil
|1044|[Longest Duplicate Substring](https://leetcode.com/problems/longest-duplicate-substring/)|[Python](./algorithms/longest-duplicate-substring/)|![Hard](https://img.shields.io/badge/-Hard-red)|
|1049|[Last Stone Weight II](https://leetcode.com/problems/last-stone-weight-ii/)|[Python](./algorithms/last-stone-weight-ii/)|![Medium](https://img.shields.io/badge/-Medium-orange)|
|1071|[Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/)|[Python](./algorithms/greatest-common-divisor-of-strings/)|![Easy](https://img.shields.io/badge/-Easy-green)|
|1081|[Smallest Subsequence of Distinct Characters](https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/)|[Python](./algorithms/smallest-subsequence-of-distinct-characters/)|![Medium](https://img.shields.io/badge/-Medium-orange)|
|1143|[Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/)|[Python](./algorithms/longest-common-subsequence/)|![Medium](https://img.shields.io/badge/-Medium-orange)|
|1232|[Check If It Is a Straight Line](https://leetcode.com/problems/check-if-it-is-a-straight-line/)|[Python](./algorithms/check-if-it-is-a-straight-line/)|![Easy](https://img.shields.io/badge/-Easy-green)|
|1277|[Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/)|[Python](./algorithms/count-square-submatrices-with-all-ones/)|![Medium](https://img.shields.io/badge/-Medium-orange)|
Expand Down
39 changes: 39 additions & 0 deletions algorithms/remove-duplicate-letters/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
Note: This question is the same as 1081: https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/
Example 1:
Input: s = "bcabc"
Output: "abc"
Example 2:
Input: s = "cbacdcbc"
Output: "acdb"
Constraints:
1 <= s.length <= 104
s consists of lowercase English letters.
"""


class Solution:
def removeDuplicateLetters(self, s: str) -> str:
last_occ = {c: i for i, c in enumerate(s)}
stack = ["!"]
visited = set()

for i, symbol in enumerate(s):
if symbol in visited:
continue
while (symbol < stack[-1] and last_occ[stack[-1]] > i):
visited.remove(stack.pop())
stack.append(symbol)
visited.add(symbol)
return "".join(stack)[1:]
38 changes: 38 additions & 0 deletions algorithms/smallest-subsequence-of-distinct-characters/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Return the lexicographically smallest subsequence of s that contains all the distinct characters of s exactly once.
Note: This question is the same as 316: https://leetcode.com/problems/remove-duplicate-letters/
Example 1:
Input: s = "bcabc"
Output: "abc"
Example 2:
Input: s = "cbacdcbc"
Output: "acdb"
Constraints:
1 <= s.length <= 1000
s consists of lowercase English letters.
"""

class Solution:
def smallestSubsequence(self, s: str) -> str:
last_occ = {c: i for i, c in enumerate(s)}
stack = ["!"]
visited = set()

for i, symbol in enumerate(s):
if symbol in visited:
continue
while (symbol < stack[-1] and last_occ[stack[-1]] > i):
visited.remove(stack.pop())
stack.append(symbol)
visited.add(symbol)
return "".join(stack)[1:]

0 comments on commit 68275c6

Please sign in to comment.