Skip to main content
Back to problems
#1903
Easy Algorithms

Largest odd number in string

Math String Greedy
67.0% acceptance
Feb 25, 2026
2562
148
You are given a string num, representing a large integer. Return the largest-valued odd integer (as a string) that is a non-empty substring of num, or an empty string "" if no odd integer exists. A substring is a contiguous sequence of characters within a string.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn largest_odd_number(num: String) -> String {
    for (i, b) in num.bytes().enumerate().rev() {
      if (b - b'0') % 2 == 1 {
        return num[..=i].to_string();
      }
    }
    String::new()
  }
}