largest-odd-number-in-string.dart (603B)
1 //Given a string num return the largest continuous 2 //integer substring that is odd. 3 4 //To solve this I iterated from the end to the start of the array 5 //each time checking to see if the current value is odd. If it is 6 //then from 0 to the current index would be the largest odd number 7 //and is thus returned. 8 9 //Time: 276ms Beats: 80% 10 //Memory: 148.7MB Beats: 40% 11 12 class Solution { 13 String largestOddNumber(String num) { 14 for(int i = num.length - 1 ; i >= 0 ; --i){ 15 if(int.parse(num[i]) % 2 == 1){ 16 return num.substring(0,i + 1); 17 } 18 } 19 return ''; 20 } 21 }