Anyone know why my code not working?


  • 0
    W

    I print out the result string1 == string2 =aa. Anyone know why it returns false?
    public class Solution {

    public boolean isPalindrome(String s) {
        if(s==null) return false;
        
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<s.length();i++){
            if(Character.isLetter(s.charAt(i))||s.charAt(i)==' '){
                sb.append(s.charAt(i));
            }
            
        }
        
        String news = sb.toString();
        if (news.length()==0||news.length()==1) return true;
        String news2 =new StringBuffer(news).reverse().toString();
        System.out.println(news+news2);
        if(news!=news2){
            return false;
        }else return true;
        
        
        
        }
    

    }


  • 1
    W

    Because you use news!=news2 to compare, it will return false, try use news.Equals(news2).


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.