Created
February 15, 2017 08:19
-
-
Save MikhailGolubtsov/fe84ce18452a2e5b4e575d85a0af72b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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