1 2 3 4 5 6 7 8 9 10 11 12 13
| class Solution { public String largestOddNumber(String num) { int oddIndex = num.length() - 1; while (oddIndex >= 0 && ((num.charAt(oddIndex) - '0') & 1) != 1) { oddIndex--; } if (oddIndex == -1) { return ""; } else { return num.substring(0, oddIndex + 1); } } }
|
References
1903. Largest Odd Number in String