Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,884 questions

51,810 answers

573 users

How to check whether each substring range (L to R) from an array of substring ranges is a palindrome in Kotlin

1 Answer

0 votes
fun isPalindrome(s: String, left: Int, right: Int): Boolean {
    var l = left
    var r = right
    
    while (l < r) {
        if (s[l] != s[r]) return false
        l++
        r--
    }
    
    return true
}

fun checkRangesForPalindrome(s: String, ranges: List<Pair<Int, Int>>) {
    for ((start, end) in ranges) {
        val substring = s.substring(start..end)
        val result = if (isPalindrome(s, start, end)) "Palindrome" else "Not palindrome"
        println("$substring: $result")
    }
}

fun main() {
    val s = "abcddcbeebc"
    val ranges = listOf(
        2 to 5,
        5 to 10,
        3 to 7
    )

    checkRangesForPalindrome(s, ranges)
}



/*
run:

cddc: Palindrome
cbeebc: Palindrome
ddcbe: Not palindrome

*/

 



answered Nov 17, 2025 by avibootz

Related questions

...