Advertisement
PrimarchVG

Simple Fun #176: Reverse Letter

Jul 19th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.71 KB | None | 0 0
  1. //Given a string str, reverse it omitting all non-alphabetic characters.
  2. //Example
  3.  
  4. //For str = "krishan", the output should be "nahsirk".
  5.  
  6. //For str = "ultr53o?n", the output should be "nortlu".
  7. //Input/Output
  8.  
  9. //    [input] string str
  10.  
  11. //    A string consists of lowercase latin letters, digits and symbols.
  12.  
  13. //    [output] a string
  14. //////////////////////////////////////////
  15. //Solution:
  16.  
  17. fun reverseLetter(str: String): String {
  18.   return str.reversed().replace("""[^a-z]""".toRegex(), "")
  19. }
  20.  
  21. // best solutions:
  22. fun reverseLetter(str: String): String {
  23.     return str.filter(Char::isLetter).reversed()
  24. }
  25.  
  26. fun reverseLetter(str: String): String {
  27.   return str.replace(Regex("[^a-zA-Z]"), "").reversed()
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement