[Codeforces] Kotlin Heroes 5: ICPC Round B. Polycarp and the Language of Gods

Kotlin Heroes 5: ICPC Round B. Polycarp and the Language of Gods

  • Time : O(n)
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fun solve(s: String) : Int {
val n = s.length
var res = 0
var i = 0
while(i < n) {
if(s[i] == 'w') {
res++;
} else if(s[i] == 'v' && (i + 1 != n && s[i + 1] == 'v')) {
res++;
i += 1;
}
i++;
}
return res;
}

fun main(args: Array<String>) {
val tc = readLine()!!
for(i in 1 .. tc.toInt()) {
val s = readLine()!!
println(solve(s))
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/19/PS/Codeforces/kotlin-heroes-5-icpc-round-b/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.