My solution based on applying srack


  • 0
    E

    class Solution {
    public boolean isPalindrome(String s) {
    int n=s.length();
    if(n<2) return true;
    Stack<Character> st=new Stack<Character>();
    int size=0;
    StringBuilder str=new StringBuilder("");
    s=s.toLowerCase();
    for(int i=0;i<n;i++){
    char c=s.charAt(i);
    if(Character.isLetterOrDigit(c)){
    str.append(c);
    size=size+1;
    }
    }
    int x=0;
    while(x<size/2){
    st.push(str.charAt(x));
    x=x+1;
    }
    if(size%2==1)
    x=x+1;
    while(x<size){
    char temp = st.pop();
    if (str.charAt(x) != temp)
    return false;
    else
    x++;
    }
    return true;
    }
    }


Log in to reply
 

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