Skip to main content
Back to problems
#3798
Easy Algorithms

Largest even number

String
69.2% acceptance
Mar 15, 2026
65
1
You are given a string s consisting only of the characters '1' and '2'. You may delete any number of characters from s without changing the order of the remaining characters. Return the largest possible resultant string that represents an even integer. If there is no such string, return the empty string "".

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn largest_even(s: String) -> String {
    // Find the last occurrence of '2' - keep everything up to and including it
    match s.rfind('2') {
      Some(idx) => s[..=idx].to_string(),
      None => String::new(),
    }
  }
}