Scala problem: define in BDD style
When Filter string unique characters Then Sort by alphabet
Solution
TODO-01: When Filter string unique characters
Use the distinct
method to find the distinct/unique characters in a String
:
"hello world".toSeq.distinct.unwrap // expected "helo wrd" "bbbaaaccc".toSeq.distinct.unwrap // expected "bac" *Note: method distinct in class StringOps is deprecated (since 2.13.0): Use `s.toSeq.distinct.unwrap` instead of `s.distinct`
TODO-02: Then Sort by alphabet
"bac".toList.sorted.mkString // expected "abc"
Reference
https://scastie.scala-lang.org/lehungio/Yvx3A6hySMW0K2ayQN48qg/29
https://www.scala-lang.org/api/current/scala/collection/StringOps.html
https://alvinalexander.com/scala/scala-strings-differences-intersection-distinct-characters/
https://stackoverflow.com/questions/46880753/scala-program-to-replace-words-in-an-alphabetical-order-with-in-a-string