Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Given a string str, reverse it omitting all non-alphabetic characters.
- //Example
- //For str = "krishan", the output should be "nahsirk".
- //For str = "ultr53o?n", the output should be "nortlu".
- //Input/Output
- // [input] string str
- // A string consists of lowercase latin letters, digits and symbols.
- // [output] a string
- //////////////////////////////////////////
- //Solution:
- fun reverseLetter(str: String): String {
- return str.reversed().replace("""[^a-z]""".toRegex(), "")
- }
- // best solutions:
- fun reverseLetter(str: String): String {
- return str.filter(Char::isLetter).reversed()
- }
- fun reverseLetter(str: String): String {
- return str.replace(Regex("[^a-zA-Z]"), "").reversed()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement