[LeetCode] Largest Odd Number in String

1903. Largest Odd Number in String

You are given a string num, representing a large integer. Return the largest-valued odd integer (as a string) that is a non-empty substring of num, or an empty string “” if no odd integer exists.

A substring is a contiguous sequence of characters within a string.

1
2
3
4
5
6
7
class Solution {
public:
string largestOddNumber(string num) {
while(num.length() && !((num.back() & 0b1111) % 2)) num.pop_back();
return num;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/06/24/PS/LeetCode/largest-odd-number-in-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.