Skip to content

leetcode/1081: monotonic stack#385

Merged
tony84727 merged 1 commit into
mainfrom
leetcode/1081
Jul 20, 2026
Merged

leetcode/1081: monotonic stack#385
tony84727 merged 1 commit into
mainfrom
leetcode/1081

Conversation

@tony84727

Copy link
Copy Markdown
Owner

No description provided.

@tony84727
tony84727 enabled auto-merge July 20, 2026 14:12

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a Rust solution for LeetCode problem 1081, "Smallest Subsequence of Distinct Characters", including tests and a README. The reviewer suggested optimizing the solution by working directly with bytes (u8) instead of char to avoid UTF-8 decoding overhead and simplify the logic.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +3 to +33
fn index(x: char) -> usize {
(x as u8 - b'a') as usize
}

impl Solution {
pub fn smallest_subsequence(s: String) -> String {
let mut last_index = [None; 26];
for (i, c) in s.char_indices() {
last_index[index(c)] = Some(i);
}
let mut used = [false; 26];
let mut answer: Vec<char> = vec![];
for (i, c) in s.char_indices() {
let ci = index(c);
if used[ci] {
continue;
}
while let Some(top) = answer.last().cloned() {
let ti = index(top);
if top > c && last_index[ti] > Some(i) {
answer.pop();
used[ti] = false;
} else {
break;
}
}
used[ci] = true;
answer.push(c);
}
answer.into_iter().collect()
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since the input string s is guaranteed to consist of lowercase English letters (ASCII), we can optimize the implementation by working directly with bytes (u8) instead of char. This avoids the overhead of UTF-8 decoding in char_indices() and simplifies the code by removing the need for Option wrapping in last_index and the helper index function.

impl Solution {
    pub fn smallest_subsequence(s: String) -> String {
        let bytes = s.as_bytes();
        let mut last_index = [0; 26];
        for (i, &b) in bytes.iter().enumerate() {
            last_index[(b - b'a') as usize] = i;
        }
        let mut used = [false; 26];
        let mut answer: Vec<u8> = vec![];
        for (i, &b) in bytes.iter().enumerate() {
            let bi = (b - b'a') as usize;
            if used[bi] {
                continue;
            }
            while let Some(&top) = answer.last() {
                let ti = (top - b'a') as usize;
                if top > b && last_index[ti] > i {
                    answer.pop();
                    used[ti] = false;
                } else {
                    break;
                }
            }
            used[bi] = true;
            answer.push(b);
        }
        String::from_utf8(answer).unwrap()
    }

@tony84727
tony84727 merged commit 7148be5 into main Jul 20, 2026
4 checks passed
@tony84727
tony84727 deleted the leetcode/1081 branch July 20, 2026 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant