-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPalíndromo.kt
42 lines (34 loc) · 819 Bytes
/
Palíndromo.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* Your task is to implement a palindrome test.
*
* A string is called a palindrome when it reads the same way left-to-right
* and right-to-left.
*
* See http://en.wikipedia.org/wiki/Palindrome
*/
package palindrome
fun isPalindrome(s: String): Boolean {
// Write your solution here
return false
}
// Solution 1
fun isPalindrome(s: String): Boolean {
return s == s.reversed()
}
// Solution 2
fun isPalindrome(s: String): Boolean {
var reversed = ""
for(i in s.indices.reversed())
reversed = "$reversed${s.charAt(i)}"
return s == reversed
}
// Solution 3
fun isPalindrome(s: String): Boolean {
val iterator = s.iterator()
var _reversed = ""
while(iterator.hasNext()){
val _char = iterator.next()
_reversed = "$_char$_reversed"
}
return s == _reversed
}