Skip to content

Commit 5c914f8

Browse files
authored
Create largest-odd-number-in-string.cpp
1 parent c0d9a25 commit 5c914f8

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

C++/largest-odd-number-in-string.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
string largestOddNumber(string num) {
7+
for (int i = size(num) - 1; i >= 0; --i) {
8+
if ((num[i] - '0') % 2) {
9+
return num.substr(0, i + 1);
10+
}
11+
}
12+
return "";
13+
}
14+
};

0 commit comments

Comments
 (0)