Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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 <= 100s consists only of the characters '1' and '2'.Problem summary: 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 "".
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"1112"
"221"
"1"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3798: Largest Even Number
class Solution {
public String largestEven(String s) {
int i = s.length();
while (i > 0 && s.charAt(i - 1) == '1') {
i--;
}
return s.substring(0, i);
}
}
// Accepted solution for LeetCode #3798: Largest Even Number
func largestEven(s string) string {
return strings.TrimRight(s, "1")
}
# Accepted solution for LeetCode #3798: Largest Even Number
class Solution:
def largestEven(self, s: str) -> str:
return s.rstrip("1")
// Accepted solution for LeetCode #3798: Largest Even Number
fn largest_even(s: String) -> String {
let cs: Vec<_> = s.chars().collect();
if let Some(p) = cs.iter().rposition(|c| *c == '2') {
cs.into_iter().take(p + 1).collect()
} else {
String::new()
}
}
fn main() {
let ret = largest_even("1112".to_string());
println!("ret={ret}");
}
#[test]
fn test() {
assert_eq!(largest_even("1112".to_string()), "1112");
assert_eq!(largest_even("221".to_string()), "22");
assert_eq!(largest_even("1".to_string()), "");
}
// Accepted solution for LeetCode #3798: Largest Even Number
function largestEven(s: string): string {
return s.replace(/1+$/, '');
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.