# 1903. Largest Odd Number in String

## Intuition

To find the **largest-valued odd integer substring**, we don't need to check every possible substring.\
Since the original string represents a number and all digits are ordered from most significant to least significant (like a real number), the largest odd-valued substring will simply be the **longest prefix** (from the start) ending at the **rightmost odd digit**.

If we find the **last odd digit** in the string while iterating from the end, the substring from the start to that index will be our answer.

## Complexity

| Space Complexity | Time Complexity |
| ---------------- | --------------- |
| $$\text{O}(1)$$  | $$\text{O}(n)$$ |

## Code

```java
public String largestOddNumber(String numberStr) {
    // Traverse from end to start
    for (int index = numberStr.length() - 1; index >= 0; --index) {
        // Convert character to integer and check if it's odd
        if ((numberStr.charAt(index) - '0') % 2 != 0) {
            // Return the substring from start to current index (inclusive)
            return numberStr.substring(0, index + 1);
        }
    }

    // No odd digit found, return empty string
    return "";
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://private-26.gitbook.io/notes/coding/easy/1903.-largest-odd-number-in-string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
