Skip to content

Instantly share code, notes, and snippets.

@MikhailGolubtsov
Created February 15, 2017 08:19
Show Gist options
  • Save MikhailGolubtsov/fe84ce18452a2e5b4e575d85a0af72b0 to your computer and use it in GitHub Desktop.
Save MikhailGolubtsov/fe84ce18452a2e5b4e575d85a0af72b0 to your computer and use it in GitHub Desktop.
object Solution {
import collection.immutable.HashMap
type IndexKey = HashMap[Char, Int]
def solve(s: String) = {
val chars = s.toCharArray()
var sorted = HashMap[IndexKey, Int]()
for {
i <- 0 to s.length - 1
} {
var indexKey = HashMap(('a' to 'z').map(_ -> 0): _*)
for (z <- i to s.length - 1) {
val c = chars(z)
indexKey = indexKey + (c -> (indexKey.get(c).getOrElse(0) + 1))
sorted = sorted + (indexKey -> (sorted.get(indexKey).getOrElse(0) + 1))
}
}
val result = sorted.values.map(x => x * (x -1) / 2).sum
println(result)
} //> solve: (s: String)Unit
def main(args: Array[String]) {
val sc = new java.util.Scanner(System.in);
for (_ <- 1 to sc.nextInt()) {
solve(sc.next())
}
} //> main: (args: Array[String])Unit
//solve("iwwhrlkpek") //> 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment