↓Skip to main content
  1. LeetCode/

LeetCode 3798: Largest Even Number

·1 min· ·
LeetCode Easy String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-01-04

  • Submissions: Accepted (first try)

Approach
#

The string has only 1s and 2s, and a number is even when its last digit is 2. More digits mean a larger number, so delete only what is necessary: strip the trailing 1s, and the rest is the largest even number. All 1s gives the empty string.

My Solution
#

class Solution:
    def largestEven(self, s: str) -> str:
        return s.rstrip("1")

I originally deleted them one at a time in a while loop; rstrip does the same.

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Deleting middle digits only makes the number shorter and smaller; only the end matters.

Interview Explanation
#

Parity depends only on the last digit, and keeping more digits is larger, so I just strip the trailing 1s. Time O(n).

Related

LeetCode 2269: Find the K-Beauty of a Number
·2 mins
LeetCode Easy Sliding-Window Math String
LeetCode 2269 note: convert the number to a string and check every length-k substring; 0 cannot be a divisor
LeetCode 3794: Reverse String Prefix
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 3794 note: one line with slicing; a hand-written two-pointer version must convert the string to a list first
LeetCode 3750: Minimum Number of Flips to Reverse Binary String
·2 mins
LeetCode Easy Two-Pointers Bit-Manipulation String
LeetCode 3750 note: compare the binary string with its reverse bit by bit; the number of mismatches is the answer
LeetCode 2264: Largest 3-Same-Digit Number in String
·1 min
LeetCode Easy String
LeetCode 2264 note: there are only ten candidates, “000” to “999”, so check from “999” down whether each appears
LeetCode 3545: Minimum Deletions for At Most K Distinct Characters
·1 min
LeetCode Easy Greedy Hash-Table String Sorting Counting
LeetCode 3545 note: reducing the number of kinds means deleting whole kinds, starting from the least frequent
LeetCode 3541: Find Most Frequent Vowel and Consonant
·1 min
LeetCode Easy Hash-Table String Counting
LeetCode 3541 note: count characters, take the maximum among vowels and among consonants and add them; a missing class counts as 0