Skip to content

Instantly share code, notes, and snippets.

@augustovictor
Created December 1, 2019 01:12
Show Gist options
  • Save augustovictor/1d48e26132efa435363dba9aebdc7ae6 to your computer and use it in GitHub Desktop.
Save augustovictor/1d48e26132efa435363dba9aebdc7ae6 to your computer and use it in GitHub Desktop.
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