Check string for palindrome

前端 未结 30 3122
悲哀的现实
悲哀的现实 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:30

    here, checking for the largest palindrome in a string, always starting from 1st char.

    public static String largestPalindromeInString(String in) {
        int right = in.length() - 1;
        int left = 0;
        char[] word = in.toCharArray();
        while (right > left && word[right] != word[left]) {
            right--;
        }
        int lenght = right + 1;
        while (right > left && word[right] == word[left]) {
    
            left++;
            right--;
    
        }
        if (0 >= right - left) {
            return new String(Arrays.copyOf(word, lenght ));
        } else {
            return largestPalindromeInString(
                    new String(Arrays.copyOf(word, in.length() - 1)));
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:33
    public class Palindromes {
        public static void main(String[] args) {
             String word = "reliefpfpfeiller";
             char[] warray = word.toCharArray(); 
             System.out.println(isPalindrome(warray));       
        }
    
        public static boolean isPalindrome(char[] word){
            if(word.length%2 == 0){
                for(int i = 0; i < word.length/2-1; i++){
                    if(word[i] != word[word.length-i-1]){
                        return false;
                    }
                }
            }else{
                for(int i = 0; i < (word.length-1)/2-1; i++){
                    if(word[i] != word[word.length-i-1]){
                        return false;
                    }
                }
            }
            return true;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:33

    I worked on a solution for a question that was marked as duplicate of this one. Might as well throw it here...

    The question requested a single line to solve this, and I took it more as the literary palindrome - so spaces, punctuation and upper/lower case can throw off the result.

    Here's the ugly solution with a small test class:

    public class Palindrome {
       public static boolean isPalendrome(String arg) {
             return arg.replaceAll("[^A-Za-z]", "").equalsIgnoreCase(new StringBuilder(arg).reverse().toString().replaceAll("[^A-Za-z]", ""));
       }
       public static void main(String[] args) {
          System.out.println(isPalendrome("hiya"));
          System.out.println(isPalendrome("star buttons not tub rats"));
          System.out.println(isPalendrome("stab nail at ill Italian bats!"));
          return;
       }
    }
    

    Sorry that it is kind of nasty - but the other question specified a one-liner.

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

    Go, Java:

    public boolean isPalindrome (String word) {
        String myWord = word.replaceAll("\\s+","");
        String reverse = new StringBuffer(myWord).reverse().toString();
        return reverse.equalsIgnoreCase(myWord);
    }
    
    isPalindrome("Never Odd or Even"); // True
    isPalindrome("Never Odd or Even1"); // False
    
    0 讨论(0)
  • 2020-11-22 03:34
     public static boolean isPalindrome(String word) {
        String str = "";
        for (int i=word.length()-1; i>=0;  i--){
            str = str + word.charAt(i);
        }
       if(str.equalsIgnoreCase(word)){
           return true;
       }else{
           return false;
       }
    
    }
    
    0 讨论(0)
  • 2020-11-22 03:35

    also a different looking solution:

    public static boolean isPalindrome(String s) {
    
            for (int i=0 , j=s.length()-1 ; i<j ; i++ , j-- ) {
    
                if ( s.charAt(i) != s.charAt(j) ) {
                    return false;
                }
            }
    
            return true;
        }
    
    0 讨论(0)
提交回复
热议问题