Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def gemStones(stones: List[String]): Int = {
- @tailrec
- def isIn(cs: List[Char], c: Char): Boolean = {
- cs match {
- case Nil => false
- case h :: t => if (h == c) true else isIn(t, c)
- }
- }
- @tailrec
- def unique(cs: List[Char], acc: List[Char]): List[Char] = {
- cs match {
- case Nil => acc
- case h :: t => if (h != ' ' && !isIn(acc, h)) unique(t, h :: acc) else unique(t, acc)
- }
- }
- val sortedStones = stones.map(s => unique(s.toList, Nil).sorted)
- (for {
- s <- sortedStones
- c <- s
- } yield (c, sortedStones.forall(isIn(_, c))))
- .filter { case (_, present) => present }
- .groupBy { case (char, _) => char }
- .keys.toList.length
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement