Created
September 30, 2022 14:14
-
-
Save lucas-marciano/075e89fb0f8f8dd32cfde0c4c809675b to your computer and use it in GitHub Desktop.
Uber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.io.*; | |
/* | |
Verify whether a long text is following the order rule defined in order string. For example the order string is "abcd", which means "a" can't appear at any position after "b", "c" and "d" in the text, "b" can't appear at any position after "c" and "d" in the text and "c" can't appear at any position after "d" in the text. if the text is "axubbxcxxdx", then the text didn't follow the rule, since "b" appears after "c" in substring "cxb" | |
a : 1 | |
x : 11 | |
b : 8 | |
c : 7 | |
d : 10 | |
*/ | |
fun main(args: Array<String>) { | |
println("Hello World!") | |
if (checkRuleList("axubbxcxbxdx", "abcd")) | |
println("rule ok") | |
else | |
println("rule not ok") | |
} | |
fun checkRuleList(list: String, rule: String) : Boolean{ | |
val rulePositions = mutableMapOf<Char, Int>() | |
var aux = 0 | |
rule.forEachIndexed { i, letter -> | |
rulePositions.put(letter, i) | |
} | |
list.forEachIndexed { _, item -> | |
val pos = rulePositions.get(item) | |
pos?.let { | |
if (pos >= aux) { | |
aux = pos | |
} else { | |
return false | |
} | |
} | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment