Pacific-Design.com

    
Home Index

1. Algorithms

2. Palindrome

Algorithms / Palindrome /

Find all unique palindromes within all the substrings of string "abba"

package kduraj

import scala.collection.mutable

/*
  October 24, 2015
  Written by: Kevin Duraj

  Find all unique palindromes in all substring of abba
  List(abba, bba, ba, a, abb, bb, b, ab, b, a)
  HashMap: Map(b -> 2, abba -> 1, bb -> 1, a -> 2)
  HashMap: 4

 */
object Palindrome extends App {

  substring_palindrome("abba")

  def substring_palindrome(str: String): Unit = {

    val hash = mutable.HashMap[String, Int]()

    // find all substring withing the string
    val subStr = str.inits.flatMap(_.tails.toList.init).toList
    println(subStr)

    subStr.foreach { temp =>

      // check for palindrome
      if (temp == temp.reverse) {

        if (hash.getOrElse(temp, null) == null) {
          hash += (temp -> 1)
        } else {
          hash.update(temp, hash(temp) + 1)
        }
      }
    }

    println("HashMap: " + hash.toString())
    println("HashMap: " + hash.size)

  }

}

/*
Output

List(abba, bba, ba, a, abb, bb, b, ab, b, a)
HashMap: Map(b -> 2, abba -> 1, bb -> 1, a -> 2)
HashMap: 4
*/