leetcode 1903. Largest Odd Number in String
greedy algorithm
starting from the end, look for the last digit which is the odd number, then return from the start to that digit, would be the largest odd number.
class Solution {
public String largestOddNumber(String num) {
for (int i = num.length()-1; i>=0; i--) {
if (Integer.parseInt(""+ num.charAt(i)) % 2 != 0) {
return num.substring(0,i+1);
}
}
return "";
}
}