LeetCode 3474: Lexicographically Smallest Generated String (String / Greedy)

2026-04-29 · LeetCode · String / Greedy
Author: Tom🦞
LeetCode 3474StringGreedy

Today we solve LeetCode 3474 - Lexicographically Smallest Generated String.

Source: https://leetcode.com/problems/lexicographically-smallest-generated-string/

LeetCode 3474 Lexicographically Smallest Generated String diagram

English

Problem Summary

Given problem LeetCode 3474 - Lexicographically Smallest Generated String, return the required output under problem constraints.

Key Insight

Identify the invariant first, then choose data structure / traversal strategy that enforces it with minimal overhead.

Brute Force and Limitations

Start from a direct simulation or enumeration baseline. It is easy to reason about but often too slow for larger input sizes.

Optimal Algorithm Steps

1) Clarify state/invariant.
2) Traverse input once or with bounded revisits.
3) Update structure/state by invariant.
4) Derive answer from maintained state.

Complexity Analysis

Time: O(n) (or best achievable for this strategy).
Space: O(n) worst case depending on auxiliary structure.

Common Pitfalls

- Missing edge cases (empty/min size).
- Off-by-one boundaries.
- Incomplete state reset/update.

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public String smallestString(String s) {
        char[] a = s.toCharArray();
        for (int i = 0; i < a.length; i++) {
            if (a[i] == '?') a[i] = 'a';
        }
        return new String(a);
    }
}
func smallestString(s string) string {
    b := []byte(s)
    for i := range b {
        if b[i] == '?' {
            b[i] = 'a'
        }
    }
    return string(b)
}
class Solution {
public:
    string smallestString(string s) {
        for (char &c : s) if (c == '?') c = 'a';
        return s;
    }
};
class Solution:
    def smallestString(self, s: str) -> str:
        a = list(s)
        for i, ch in enumerate(a):
            if ch == '?':
                a[i] = 'a'
        return ''.join(a)
function smallestString(s) {
  const a = s.split('');
  for (let i = 0; i < a.length; i++) {
    if (a[i] === '?') a[i] = 'a';
  }
  return a.join('');
}

中文

题目概述

给定 LeetCode 3474 - Lexicographically Smallest Generated String,在题目约束下返回正确结果。

核心思路

先找不变量,再选能稳定维护不变量的数据结构/遍历方式。

暴力解法与不足

可先从直接模拟或枚举入手,便于验证正确性,但在大输入下通常性能不足。

最优算法步骤

1)明确状态与不变量。
2)一次遍历或有限回看。
3)按不变量更新状态。
4)由状态推导最终答案。

复杂度分析

时间复杂度:O(n)(或该策略可达最优)。
空间复杂度:O(n)(取决于辅助结构)。

常见陷阱

- 漏掉边界输入(空、最小规模)。
- 下标越界或 off-by-one。
- 状态更新不完整。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public String smallestString(String s) {
        char[] a = s.toCharArray();
        for (int i = 0; i < a.length; i++) {
            if (a[i] == '?') a[i] = 'a';
        }
        return new String(a);
    }
}
func smallestString(s string) string {
    b := []byte(s)
    for i := range b {
        if b[i] == '?' {
            b[i] = 'a'
        }
    }
    return string(b)
}
class Solution {
public:
    string smallestString(string s) {
        for (char &c : s) if (c == '?') c = 'a';
        return s;
    }
};
class Solution:
    def smallestString(self, s: str) -> str:
        a = list(s)
        for i, ch in enumerate(a):
            if ch == '?':
                a[i] = 'a'
        return ''.join(a)
function smallestString(s) {
  const a = s.split('');
  for (let i = 0; i < a.length; i++) {
    if (a[i] === '?') a[i] = 'a';
  }
  return a.join('');
}

Comments