commit d1d51fa34762f74274cc74d0988dd2d6eb7bcbb6 parent 5c96deee302a46f95974ff81b97774a243fa46b7 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Tue, 6 Jun 2023 23:03:06 -0500 Completed largest odd number using greedy algorithm Diffstat:
| A | largest-odd-number-in-string/largest-odd-number-in-string.dart | | | 21 | +++++++++++++++++++++ |
1 file changed, 21 insertions(+), 0 deletions(-)
diff --git a/largest-odd-number-in-string/largest-odd-number-in-string.dart b/largest-odd-number-in-string/largest-odd-number-in-string.dart @@ -0,0 +1,21 @@ +//Given a string num return the largest continuous +//integer substring that is odd. + +//To solve this I iterated from the end to the start of the array +//each time checking to see if the current value is odd. If it is +//then from 0 to the current index would be the largest odd number +//and is thus returned. + +//Time: 276ms Beats: 80% +//Memory: 148.7MB Beats: 40% + +class Solution { + String largestOddNumber(String num) { + for(int i = num.length - 1 ; i >= 0 ; --i){ + if(int.parse(num[i]) % 2 == 1){ + return num.substring(0,i + 1); + } + } + return ''; + } +}