object Solution { class Query() case class Query1(startIndex: Int, endIndex: Int, shift: Int) extends Query case class Query2(startIndex: Int, endIndex: Int) extends Query def main(args: Array[String]): Unit = { val sc = new java.util.Scanner(System.in) val n = sc.nextInt() val q = sc.nextInt() var s = sc.next() val queries = new Array[Query](q) for (i <- 0 until q) { val queryType = sc.nextInt() if (queryType == 1) { queries(i) == Query1(sc.nextInt(), sc.nextInt(), sc.nextInt()) } else { queries(i) = Query2(sc.nextInt(), sc.nextInt()) } } for (q <- queries) { q match { case q1: Query1 => { s = shift(q1, s) } case q2: Query2 => { println(numPalindromes(q2, s)) } } } } def shift(q: Query1, s: String): String = { val start = q.startIndex val end = q.endIndex val shift = q.shift % 26 val characters: List[Char] = List('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' , 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z') val charNum = (characters zip (0 to 25)).toMap val numChar = ((0 to 25) zip characters).toMap def update(i: Int): Char = { if (start <= i && i <= end) { numChar((charNum(s(i)) + shift) % 26) } else { s(i) } } val res = s.indices.map(update(_)).toString() res } def numPalindromes(q: Query2, fullString: String): Int = { val start = q.startIndex val end = q.endIndex val s = fullString.substring(start, end+1) def getSubSequences(s : String) : List[String] = { if (s.length == 0) {List("")} else { val head : Char = s.head val rec = getSubSequences(s.tail) rec ++ rec.map(s => head +: s) } } def isPalindromic(sub : String) : Boolean = { val characters: List[Char] = List('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' , 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z') val charCounts: Map[Char, Int] = (characters.map(ch => (ch, sub.count(_ == ch)))).toMap charCounts.values.count(c => c % 2 ==1) <= 1 } val subsequences = getSubSequences(s).filter(!_.isEmpty) subsequences.count(isPalindromic(_)) } }