0

this is the problem A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.

Given a string s, return true if it is a palindrome, or false otherwise.

Example 1:

Input: s = "A man, a plan, a canal: Panama" Output: true Explanation: "amanaplanacanalpanama" is a palindrome. Example 2:

Input: s = "race a car" Output: false Explanation: "raceacar" is not a palindrome.

myCode

class Solution {
      fun isPalindrome(s:String):Boolean {
    var s1 = s.toLowerCase()
    var myStringBuilder = StringBuilder()
    var n = s1.length-1
    var n1=myStringBuilder.length

    for ( i in 0..n) {
        if (Character.isLetterOrDigit(s1[i])) {
            myStringBuilder.append(s1[i])
        }
    }
     for( i in 0 .. (n1/2)-1){
            if(myStringBuilder[i] != myStringBuilder[n1-i-1]){
                return false
            }
     } 
        return true
}
}

the first case passed but this is not passed as per the result Input: s = "race a car result true expected is false

2 Answers 2

1

You're initialising n1 too early:

// create an -empty- StringBuilder
var myStringBuilder = StringBuilder()
...
// since it's empty, n1 == 0
var n1=myStringBuilder.length

You're setting it to the length of the StringBuilder contents before you've actually put anything in it. This is a simple value you're setting, it's not a reference to the length getter that will give the current value when you access it. You set it once and that's its value forever.

So your last loop, the one that checks if it's a palindrome or not, never actually runs:

// since n1 is 0, this is for (i in 0..0)
for( i in 0 .. (n1/2)-1){

You can fix it by initialising n1 when you've finished adding your content to the StringBuilder, so you can get its final length:

for ( i in 0..n) {
    if (Character.isLetterOrDigit(s1[i])) {
        myStringBuilder.append(s1[i])
    }
}

// StringBuilder is complete, grab its final length
var n1 = myStringBuilder.length

// now you can use it
for (i in 0..(n1/2)-1) {

Just fyi, there's also an until operator that works like .. except it doesn't include the last value of the range. So you can write

for (i in 0 until (n1/2))

if you want!

Sign up to request clarification or add additional context in comments.

2 Comments

i have edited the code but still get the same result class Solution { fun isPalindrome(s:String):Boolean { var s1 = s.toLowerCase() var myStringBuilder = StringBuilder() var n = s1.length for ( i in 0 until n) { if (Character.isLetterOrDigit(s1[i])) { myStringBuilder.append(s1[i]) } } var n1=myStringBuilder.length for( i in 0 until (n1/2)-1){ if(myStringBuilder[i] != myStringBuilder[n1-i-1]){ return false } } return true } }
@Beginnerdeveloper I can't really read that, but I see you're using until while still doing the -1. That's the point of until, instead of doing start..end-1 you can do start until end - the until basically includes the -1 by skipping the last element, unlike ... Your code works with the change I suggested, if you're still having trouble compare what you've got to this: pl.kotl.in/RFhr4Z1MC
1

You can use this simple solution.

fun isPalindrome(s:String):Boolean {
    val str = s.filter { it.isLetterOrDigit() }.lowercase()
    for (i in 0..str.length/2 ){
        if (str[i]!=str[str.length-i-1])
            return false
    }
    return true
}

Edit: By the @cactustictacs comment, you can do this in much more simple way.

fun isPalindrome(s:String):Boolean {
    val str = s.filter { it.isLetterOrDigit() }.lowercase()
    return str == str.reversed()
}

1 Comment

You can do return str == str.reversed() too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.