Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*
- var sc: Scanner = Scanner(System.`in`)
- fun main() {
- val n: Int = sc.nextInt()
- val s: String = sc.next()
- val res = smallestSubstring(n, s)
- println(res)
- }
- fun smallestSubstring(n: Int, s: String): Int {
- val temp: CharArray = s.toCharArray()
- // Check for length of 2
- for (i in 0 until n - 1) {
- if (temp[i] == 'x' && temp[i + 1] == 'x')
- return 2
- }
- // Check for length of 3
- for (i in 0 until n - 2) {
- if (temp[i] == 'x' && temp[i + 2] == 'x')
- return 3
- }
- // Check for length of 4
- for (i in 0 until n - 3) {
- if (temp[i] == 'x' && temp[i + 3] == 'x' && temp[i + 1] != temp[i + 2])
- return 4
- }
- // Check for length of 7
- for (i in 0 until n - 6) {
- if (temp[i] == 'x' && temp[i + 3] == 'x' && temp[i + 6] == 'x') {
- var countY = 0
- var countZ = 0
- for (j in i + 1..i + 5) {
- if (temp[j] == 'y')
- countY++
- else if (temp[j] == 'z')
- countZ++
- }
- if (countY == 2 && countZ == 2)
- return 7
- }
- }
- return -1
- }
Add Comment
Please, Sign In to add comment