Scala Iterative solution


  • 0
    K
    object Solution {
        def countSubstrings(s: String): Int = {
            var res = 0
            for(i <- 0 to s.length - 1; j <- i + 1 to s.length)
                s.substring(i, j) match {
                    case p if isPalindrome(p) => res += 1
                    case _ => 
                }
            res
        }    
        
        def isPalindrome(s: String): Boolean = {
            for(i <- 0 to s.length / 2)
                s.charAt(i) == s.charAt(s.length - 1 - i) match {
                    case false => return false
                    case _ =>
                }
            true
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.