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,870 questions

51,793 answers

573 users

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

1 Answer

0 votes
object PalindromeChecker {

  // Check if a substring of s from left to right (inclusive) is a palindrome
  def 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 += 1
      r -= 1
    }
    
    true
  }

  // Check and print whether substrings defined by ranges are palindromes
  def checkRangesForPalindrome(s: String, ranges: Seq[(Int, Int)]): Unit = {
    for ((start, end) <- ranges) {
      val substring = s.slice(start, end + 1)
      val result = if (isPalindrome(s, start, end)) "Palindrome" else "Not palindrome"
      println(s"$substring: $result")
    }
  }

  def main(args: Array[String]): Unit = {
    val s = "abcddcbeebc"
    val ranges = Seq((2, 5), (5, 10), (3, 7))

    checkRangesForPalindrome(s, ranges)
  }
}



/*
run:

cddc: Palindrome
cbeebc: Palindrome
ddcbe: Not palindrome

*/

 



answered Nov 17, 2025 by avibootz

Related questions

...