-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanToInteger.kt
More file actions
43 lines (39 loc) · 934 Bytes
/
RomanToInteger.kt
File metadata and controls
43 lines (39 loc) · 934 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package leetcode
/**
* Problem description on [LeetCode](https://leetcode.com/problems/roman-to-integer/)
*/
class RomanToInteger {
private val mapRoman = mapOf(
"IV" to 4,
"IX" to 9,
"XL" to 40,
"XC" to 90,
"CD" to 400,
"CM" to 900,
"I" to 1,
"V" to 5,
"X" to 10,
"L" to 50,
"C" to 100,
"D" to 500,
"M" to 1000
)
private fun nextRoman(s: String): String? {
mapRoman.forEach {
if (s.startsWith(it.key)) {
return it.key
}
}
return null
}
fun romanToInt(s: String): Int {
var substr = s
var result = 0
while (substr.isNotEmpty()) {
val roman = nextRoman(substr) ?: return 0
result += mapRoman[roman]!!
substr = substr.substring(roman.length)
}
return result
}
}