Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. def gemStones(stones: List[String]): Int = {
  2. @tailrec
  3. def isIn(cs: List[Char], c: Char): Boolean = {
  4. cs match {
  5. case Nil => false
  6. case h :: t => if (h == c) true else isIn(t, c)
  7. }
  8. }
  9.  
  10. @tailrec
  11. def unique(cs: List[Char], acc: List[Char]): List[Char] = {
  12. cs match {
  13. case Nil => acc
  14. case h :: t => if (h != ' ' && !isIn(acc, h)) unique(t, h :: acc) else unique(t, acc)
  15. }
  16. }
  17.  
  18. val sortedStones = stones.map(s => unique(s.toList, Nil).sorted)
  19. (for {
  20. s <- sortedStones
  21. c <- s
  22. } yield (c, sortedStones.forall(isIn(_, c))))
  23. .filter { case (_, present) => present }
  24. .groupBy { case (char, _) => char }
  25. .keys.toList.length
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement