πŸ€– Greedy Robot & Lexicographically Smallest String | LeetCode 2434 (C++ | JavaScript | Python) - Featured Image
Programming4 min read

πŸ€– Greedy Robot & Lexicographically Smallest String | LeetCode 2434 (C++ | JavaScript | Python)

Hey, tech explorers! 🌟 Today, let’s dive into a fun and slightly sneaky greedy problem β€” LeetCode 2434: Using a Robot to Print the Lexicographically Smallest String. We'll crack it open using an intuitive stack-based greedy approach. πŸ’‘

πŸ“œ Problem Overview

LeetCode 2434: Using a Robot to Print the Lexicographically Smallest String

Given a string s, you control a robot that maintains an auxiliary string t (initially empty). The robot can perform two operations:

  1. Move operation: Remove the first character from s and append it to the end of t

  2. Write operation: Remove the last character from t and write it to the result

The goal is to determine the lexicographically smallest string that can be written by performing these operations until both s and t are empty.

Example Walkthrough

Input: s = "bac"
Step 1: Move 'b' from s to t β†’ s="ac", t="b"
Step 2: Move 'a' from s to t β†’ s="c", t="ba" 
Step 3: Write 'a' from t β†’ s="c", t="b", result="a"
Step 4: Move 'c' from s to t β†’ s="", t="bc"
Step 5: Write 'b' from t β†’ s="", t="c", result="ab"
Step 6: Write 'c' from t β†’ s="", t="", result="abc"
Output: "abc"

🧠 Algorithm Intuition

The key insight is to use a greedy strategy: we should write a character from t to the result when it's smaller than or equal to any character we might encounter later in s. This requires:

  1. Preprocessing: Calculate the minimum character from each position to the end of s

  2. Greedy decision: At each step, write from t if the top character is ≀ the minimum remaining character in s

  3. Stack simulation: Use a stack to represent the auxiliary string t

βš™οΈ Solution Implementation

C++ Solution

class Solution {
public:
    string robotWithString(string s) {
        int n = s.size();
        vector<char> minSuffix(n);
        
        // Build minimum suffix array
        minSuffix[n-1] = s[n-1];
        for (int i = n-2; i >= 0; i--) {
            minSuffix[i] = min(s[i], minSuffix[i+1]);
        }
        
        string result;
        stack<char> t;
        result.reserve(n); // Optimize memory allocation
        
        for (int i = 0; i < n; i++) {
            t.push(s[i]);
            
            // Write from stack while beneficial
            while (!t.empty() && (i == n-1 || t.top() <= minSuffix[i+1])) {
                result += t.top();
                t.pop();
            }
        }
        
        return result;
    }
};

JavaScript Solution

function robotWithString(s) {
    const n = s.length;
    const minSuffix = new Array(n);
    
    // Build minimum suffix array
    minSuffix[n-1] = s[n-1];
    for (let i = n-2; i >= 0; i--) {
        minSuffix[i] = s[i] < minSuffix[i+1] ? s[i] : minSuffix[i+1];
    }
    
    const result = [];
    const stack = [];
    
    for (let i = 0; i < n; i++) {
        stack.push(s[i]);
        
        // Write from stack while beneficial
        while (stack.length && (i === n-1 || stack[stack.length-1] <= minSuffix[i+1])) {
            result.push(stack.pop());
        }
    }
    
    return result.join('');
}

Python Solution

def robotWithString(s: str) -> str:
    n = len(s)
    min_suffix = [''] * n
    
    # Build minimum suffix array
    min_suffix[-1] = s[-1]
    for i in range(n-2, -1, -1):
        min_suffix[i] = min(s[i], min_suffix[i+1])
    
    result = []
    stack = []
    
    for i in range(n):
        stack.append(s[i])
        
        # Write from stack while beneficial
        while stack and (i == n-1 or stack[-1] <= min_suffix[i+1]):
            result.append(stack.pop())
    
    return ''.join(result)

Test Cases & Analysis

Basic Examples

Input: s = "zza"
Output: "azz"
Explanation: All characters go to stack first, then pop in optimal order

Input: s = "bac" 
Output: "abc"
Explanation: Strategic popping when 'a' is encountered

Input: s = "bdda"
Output: "addb"
Explanation: Build stack, then unwind optimally

Edge Cases

Input: s = "a"
Output: "a"
Explanation: Single character case

Input: s = "dcba"
Output: "abcd" 
Explanation: Reverse order input produces sorted output

Input: s = "aaaa"
Output: "aaaa"
Explanation: All characters identical

Complexity Analysis

  • Time Complexity: O(n) where n is the length of string s

    • Each character is pushed to the stack exactly once

    • Each character is popped from the stack at most once

    • Preprocessing takes O(n) time

  • Space Complexity: O(n) for the stack and minimum suffix array

Key Insights

  1. Greedy Choice: Always write the smallest available character when it won't hurt future decisions

  2. Look-ahead: The minimum suffix array enables optimal decision-making

  3. Stack Properties: LIFO nature of the auxiliary string t naturally maps to stack operations

  4. Amortized Analysis: Despite nested loops, each character is processed exactly twice (push + pop)

Common Pitfalls

  • Forgetting edge cases: Handle the last character (i == n-1) correctly

  • Incorrect comparison: Compare with minimum suffix, not current character

  • Stack underflow: Always check if stack is empty before popping

This problem elegantly demonstrates how greedy algorithms can be enhanced with preprocessing and appropriate data structures to achieve optimal solutions efficiently.

✨ Wrap Up

This problem beautifully blends greedy logic with stack simulation! Perfect for interviews and leveling up your string intuition. πŸ“ˆ

If you found this helpful, smash that ❀️ and follow for more intuitive breakdowns!

Happy coding! πŸš€πŸ’»

omshree0709@gmail.com

πŸš€ Front-End Developer | UI/UX Enthusiast | EdTech Innovator

Posted by

β€Œ
β€Œ
β€Œ
β€Œ

Subscribe to our newsletter

Join 2,000+ subscribers

Stay in the loop with everything you need to know.

We care about your data in our privacy policy

Background shadow leftBackground shadow right

Have something to share?

Write on the platform and dummy copy content

Be Part of Something Big

Shifters, a developer-first community platform, is launching soon with all the features. Don't miss out on day one access. Join the waitlist: