Check string for palindrome

前端 未结 30 3124
悲哀的现实
悲哀的现实 2020-11-22 02:47

A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction.

To check whether a word is a palindrome I get th

相关标签:
30条回答
  • 2020-11-22 03:35
    public class palindrome {
    public static void main(String[] args) {
        StringBuffer strBuf1 = new StringBuffer("malayalam");
        StringBuffer strBuf2 = new StringBuffer("malayalam");
        strBuf2.reverse();
    
    
        System.out.println(strBuf2);
        System.out.println((strBuf1.toString()).equals(strBuf2.toString()));
        if ((strBuf1.toString()).equals(strBuf2.toString()))
            System.out.println("palindrome");
        else
            System.out.println("not a palindrome");
    }
    

    }

    0 讨论(0)
  • 2020-11-22 03:35

    Checking palindrome for first half of the string with the rest, this case assumes removal of any white spaces.

    public int isPalindrome(String a) {
            //Remove all spaces and non alpha characters
            String ab = a.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
            //System.out.println(ab);
    
            for (int i=0; i<ab.length()/2; i++) {
                if(ab.charAt(i) != ab.charAt((ab.length()-1)-i)) {
                    return 0;
                }
            }   
            return 1;
        }
    
    0 讨论(0)
  • 2020-11-22 03:37
    import java.util.Scanner;
    
    
    public class Palindrom {
    
        public static void main(String []args)
        {
            Scanner in = new Scanner(System.in);
            String str= in.nextLine();
            int x= str.length();
    
            if(x%2!=0)
            {
                for(int i=0;i<x/2;i++)
                {
    
                    if(str.charAt(i)==str.charAt(x-1-i))
                    {
                        continue;
                    }
                    else 
                    {
                        System.out.println("String is not a palindrom");
                        break;
                    }
                }
            }
            else
            {
                for(int i=0;i<=x/2;i++)
                {
                    if(str.charAt(i)==str.charAt(x-1-i))
                    {
                        continue;
                    }
                    else 
                    {
                        System.out.println("String is not a palindrom");
                        break;
                    }
    
                }
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 03:38

    Considering not letters in the words

    public static boolean palindromeWords(String s ){
    
            int left=0;
            int right=s.length()-1;
    
            while(left<=right){
    
                while(left<right && !Character.isLetter(s.charAt(left))){
                    left++;
                }
                while(right>0 && !Character.isLetter(s.charAt(right))){
                    right--;
                }
    
                if((s.charAt(left++))!=(s.charAt(right--))){
                    return false;
                }
            }
            return true;
        }
    

    ———

    @Test
    public void testPalindromeWords(){
        assertTrue(StringExercise.palindromeWords("ece"));
        assertTrue(StringExercise.palindromeWords("kavak"));
        assertFalse(StringExercise.palindromeWords("kavakdf"));
        assertTrue(StringExercise.palindromeWords("akka"));
        assertTrue(StringExercise.palindromeWords("??e@@c_--e"));
    }
    
    0 讨论(0)
  • 2020-11-22 03:39

    Here my analysis of the @Greg answer: componentsprogramming.com/palindromes


    Sidenote: But, for me it is important to do it in a Generic way. The requirements are that the sequence is bidirectionally iterable and the elements of the sequence are comparables using equality. I don't know how to do it in Java, but, here is a C++ version, I don't know a better way to do it for bidirectional sequences.

    template <BidirectionalIterator I> 
        requires( EqualityComparable< ValueType<I> > ) 
    bool palindrome( I first, I last ) 
    { 
        I m = middle(first, last); 
        auto rfirst = boost::make_reverse_iterator(last); 
        return std::equal(first, m, rfirst); 
    } 
    

    Complexity: linear-time,

    • If I is RandomAccessIterator: floor(n/2) comparissons and floor(n/2)*2 iterations

    • If I is BidirectionalIterator: floor(n/2) comparissons and floor(n/2)*2 iterations plus (3/2)*n iterations to find the middle ( middle function )

    • storage: O(1)

    • No dymamic allocated memory


    0 讨论(0)
  • 2020-11-22 03:39

    Code Snippet:

    import java.util.Scanner;
    
     class main
     {
        public static void main(String []args)
        {
           Scanner sc = new Scanner(System.in);
           String str = sc.next();
           String reverse = new StringBuffer(str).reverse().toString();
    
            if(str.equals(reverse))
                System.out.println("Pallindrome");
            else
                System.out.println("Not Pallindrome");
         }
    }
    
    0 讨论(0)
提交回复
热议问题