-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
23 lines (19 loc) · 746 Bytes
/
Solution.cs
File metadata and controls
23 lines (19 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
namespace LeetCode.Problem1903{
//1903. Largest Odd Number in String
//https://leetcode.com/problems/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.
*/
public class Solution {
public string LargestOddNumber(string num) {
if (Convert.ToInt32(num.LastOrDefault()) % 2 != 0)
return num;
var index = num.LastIndexOf(num.Where(p=> Convert.ToInt32(p) % 2 != 0).LastOrDefault());
if (index == -1)
return "";
return num.Remove(index+1);
}
}
}