Talentd
FleetCode

3798. Largest Even Number

Easy68.5% acceptance

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 "".

Example 1:

Input: s = "1112"

Output: "1112"

Explanation:

The string already represents the largest possible even number, so no deletions are needed.

Example 2:

Input: s = "221"

Output: "22"

Explanation:

Deleting '1' results in the largest possible even number which is equal to 22.

Example 3:

Input: s = "1"

Output: ""

Explanation:

There is no way to get an even number.

Constraints:

  • 1 <= s.length <= 100
  • s consists only of the characters '1' and '2'.

Topics

Hints open one at a time

Hint 1
A number ending with '1' is odd.
Hint 2
Find the last '2' in s. If none, return "". Otherwise return the prefix up to and including that '2'.
2:37
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def largestEven(self, s: str) -> str:
3
Starter codePython 3Spaces: 4
Examples
Input
"1112"
Input
"221"
Input
"1"
Your code is saved on this device.Solution