[LeetCode] Largest Even Number

3798. Largest Even Number

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

1
2
3
4
5
6
7
class Solution {
public:
string largestEven(string s) {
while(s.length() and s.back() == '1') s.pop_back();
return s;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/largest-even-number/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.