Created
December 1, 2019 01:12
-
-
Save augustovictor/1d48e26132efa435363dba9aebdc7ae6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
fun reverseStrRecursively(str: String): String { | |
return if (str.length == 1) str | |
else reverseStrRecursively(str.substring(1, str.length)) + str.substring(0, 1) | |
} | |
fun reverseStrIteratively(str: String): String { | |
val result = mutableListOf<String>() | |
var i = str.length | |
do { | |
result.add(str.substring(i-1, i)) | |
i-- | |
} while (result.size != str.length) | |
return result.joinToString("") | |
} | |
} | |
fun main() { | |
println(Solution().reverseStrRecursively("abcd")) | |
println(Solution().reverseStrIteratively("abcd")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment