简体   繁体   English

检查回文字符串

[英]Check string for palindrome

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 the char array of the word and compare the chars.为了检查一个词是否是回文,我得到了这个词的 char 数组并比较了这些字符。 I tested it and it seems to work.我测试了它,它似乎工作。 However I want to know if it is right or if there is something to improve.但是我想知道它是否正确或者是否有需要改进的地方。

Here is my code:这是我的代码:

public class Aufg1 {
    public static void main(String[] args) {
        String wort = "reliefpfpfeiller";
        char[] warray = wort.toCharArray(); 
        System.out.println(istPalindrom(warray));       
    }

    public static boolean istPalindrom(char[] wort){
        boolean palindrom = false;
        if(wort.length%2 == 0){
            for(int i = 0; i < wort.length/2-1; i++){
                if(wort[i] != wort[wort.length-i-1]){
                    return false;
                }else{
                    palindrom = true;
                }
            }
        }else{
            for(int i = 0; i < (wort.length-1)/2-1; i++){
                if(wort[i] != wort[wort.length-i-1]){
                    return false;
                }else{
                    palindrom = true;
                }
            }
        }
        return palindrom;
    }
}

Why not just:为什么不只是:

public static boolean istPalindrom(char[] word){
    int i1 = 0;
    int i2 = word.length - 1;
    while (i2 > i1) {
        if (word[i1] != word[i2]) {
            return false;
        }
        ++i1;
        --i2;
    }
    return true;
}

Example:例子:

Input is "andna".输入是“andna”。
i1 will be 0 and i2 will be 4. i1 为 0,i2 为 4。

First loop iteration we will compare word[0] and word[4] .第一次循环迭代我们将比较word[0]word[4] They're equal, so we increment i1 (it's now 1) and decrement i2 (it's now 3).它们相等,所以我们增加 i1(现在是 1)并减少 i2(现在是 3)。
So we then compare the n's.所以我们然后比较n。 They're equal, so we increment i1 (it's now 2) and decrement i2 (it's 2).它们相等,所以我们增加 i1(现在是 2)并减少 i2(现在是 2)。
Now i1 and i2 are equal (they're both 2), so the condition for the while loop is no longer true so the loop terminates and we return true.现在 i1 和 i2 相等(它们都是 2),所以 while 循环的条件不再为真,所以循环终止,我们返回真。

You can check if a string is a palindrome by comparing it to the reverse of itself:您可以通过将字符串与自身的反向进行比较来检查字符串是否为回文:

public static boolean isPalindrome(String str) {
    return str.equals(new StringBuilder(str).reverse().toString());
}

or for versions of Java earlier than 1.5,或者对于早于 1.5 的 Java 版本,

public static boolean isPalindrome(String str) {
    return str.equals(new StringBuffer().append(str).reverse().toString());
}

EDIT: @FernandoPelliccioni provided a very thorough analysis of the efficiency (or lack thereof) of this solution, both in terms of time and space.编辑: @FernandoPelliccioni 提供对该解决方案在时间和空间方面的效率(或缺乏效率)的非常彻底的分析 If you're interested in the computational complexity of this and other possible solutions to this question, please read it!如果您对此问题的计算复杂度和其他可能的解决方案感兴趣,请阅读它!

A concise version, that doesn't involve (inefficiently) initializing a bunch of objects:一个简洁的版本,不涉及(低效)初始化一堆对象:

boolean isPalindrome(String str) {    
    int n = str.length();
    for( int i = 0; i < n/2; i++ )
        if (str.charAt(i) != str.charAt(n-i-1)) return false;
    return true;    
}

Alternatively, recursion .或者,递归

For anybody who is looking for a shorter recursive solution, to check if a given string satisfies as a palindrome:对于任何正在寻找更短递归解决方案的人,要检查给定的字符串是否满足回文:

private boolean isPalindrome(String s) {
    int length = s.length();

    if (length < 2) // If the string only has 1 char or is empty
        return true;
    else {
        // Check opposite ends of the string for equality
        if (s.charAt(0) != s.charAt(length - 1))
            return false;
        // Function call for string with the two ends snipped off
        else
            return isPalindrome(s.substring(1, length - 1));
    }
}

OR even shorter , if you'd like:或者,如果你愿意:

private boolean isPalindrome(String s) {
    int length = s.length();
    if (length < 2) return true;
    return s.charAt(0) != s.charAt(length - 1) ? false :
            isPalindrome(s.substring(1, length - 1));
}

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

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;
    }
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;
    }
}

And here a complete Java 8 streaming solution.这是一个完整的 Java 8流媒体解决方案。 An IntStream provides all indexes til strings half length and then a comparision from the start and from the end is done. IntStream提供所有索引直到字符串的一半长度,然后从头到尾进行比较。

public static void main(String[] args) {
    for (String testStr : Arrays.asList("testset", "none", "andna", "haah", "habh", "haaah")) {
        System.out.println("testing " + testStr + " is palindrome=" + isPalindrome(testStr));
    }
}

public static boolean isPalindrome(String str) {
    return IntStream.range(0, str.length() / 2)
            .noneMatch(i -> str.charAt(i) != str.charAt(str.length() - i - 1));
}

Output is:输出是:

testing testset is palindrome=true
testing none is palindrome=false
testing andna is palindrome=true
testing haah is palindrome=true
testing habh is palindrome=false
testing haaah is palindrome=true
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");
}

} }

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;
    }

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.抱歉,这有点令人讨厌 - 但另一个问题指定了一个单线。

I'm new to java and I'm taking up your question as a challenge to improve my knowledge.我是 Java 新手,我将您的问题作为提高我知识的挑战。

import java.util.ArrayList;
import java.util.List;

public class PalindromeRecursiveBoolean {

    public static boolean isPalindrome(String str) {

        str = str.toUpperCase();
        char[] strChars = str.toCharArray();

        List<Character> word = new ArrayList<>();
        for (char c : strChars) {
            word.add(c);
        }

        while (true) {
            if ((word.size() == 1) || (word.size() == 0)) {
                return true;
            }
            if (word.get(0) == word.get(word.size() - 1)) {
                word.remove(0);
                word.remove(word.size() - 1);
            } else {
                return false;

            }

        }
    }
}
  1. If the string is made of no letters or just one letter, it is a palindrome.如果字符串没有字母或只有一个字母,则它是回文。
  2. Otherwise, compare the first and last letters of the string.否则,比较字符串的第一个和最后一个字母。
    • If the first and last letters differ, then the string is not a palindrome如果第一个和最后一个字母不同,则该字符串不是回文
    • Otherwise, the first and last letters are the same.否则,第一个和最后一个字母是相同的。 Strip them from the string, and determine whether the string that remains is a palindrome.从字符串中剥离它们,并确定剩余的字符串是否为回文。 Take the answer for this smaller string and use it as the answer for the original string then repeat from 1 .取这个较小字符串的答案并将其用作原始字符串的答案,然后从1开始重复。

Try this out :试试这个:

import java.util.*;
    public class str {

        public static void main(String args[])
        {
          Scanner in=new Scanner(System.in);
          System.out.println("ENTER YOUR STRING: ");
          String a=in.nextLine();
          System.out.println("GIVEN STRING IS: "+a);
          StringBuffer str=new StringBuffer(a);
          StringBuffer str2=new StringBuffer(str.reverse());
          String s2=new String(str2);
          System.out.println("THE REVERSED STRING IS: "+str2);
            if(a.equals(s2))    
                System.out.println("ITS A PALINDROME");
            else
                System.out.println("ITS NOT A PALINDROME");
            }
    }
public boolean isPalindrome(String abc){
    if(abc != null && abc.length() > 0){
        char[] arr = abc.toCharArray();
        for (int i = 0; i < arr.length/2; i++) {
            if(arr[i] != arr[arr.length - 1 - i]){
                return false;
            }
        }
        return true;
    }
    return false;
}

Another way is using char Array另一种方法是使用 char Array

public class Palindrome {

public static void main(String[] args) {
    String str = "madam";
    if(isPalindrome(str)) {
        System.out.println("Palindrome");
    } else {
        System.out.println("Not a Palindrome");
    }
}

private static boolean isPalindrome(String str) {
    // Convert String to char array
    char[] charArray = str.toCharArray();  
    for(int i=0; i < str.length(); i++) {
        if(charArray[i] != charArray[(str.length()-1) - i]) {
            return false;
        }
    }
    return true;
}

} }

Here my analysis of the @Greg answer: componentsprogramming.com/palindromes这是我对@Greg 回答的分析: 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.我不知道如何在 Java 中执行此操作,但是,这是一个 C++ 版本,我不知道对双向序列执行此操作的更好方法。

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如果我是 RandomAccessIterator: floor(n/2) 比较和 floor(n/2)*2 迭代

  • 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 )如果我是双向迭代器: floor(n/2) 比较和 floor(n/2)*2 次迭代加上 (3/2)*n 次迭代以找到中间(中间函数)

  • storage: O(1)存储:O(1)

  • No dymamic allocated memory没有动态分配的内存


Recently I wrote a palindrome program which doesn't use StringBuilder.最近我写了一个不使用 StringBuilder 的回文程序。 A late answer but this might come in handy to some people.一个迟到的答案,但这可能对某些人有用。

public boolean isPalindrome(String value) {
    boolean isPalindrome = true;
    for (int i = 0 , j = value.length() - 1 ; i < j ; i ++ , j --) {
        if (value.charAt(i) != value.charAt(j)) {
            isPalindrome = false;
        }
    }
    return isPalindrome;
}

Using stack, it can be done like this使用堆栈,可以这样完成

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str=in.nextLine();
        str.replaceAll("\\s+","");
        //System.out.println(str);
        Stack<String> stack=new Stack<String>();
        stack.push(str);
        String str_rev=stack.pop();
        if(str.equals(str_rev)){
            System.out.println("Palindrome"); 
        }else{
             System.out.println("Not Palindrome");
        }
    }
}
 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;
   }

}

Amazing how many different solutions to such a simple problem exist!如此简单的问题竟然有这么多不同的解决方案! Here's another one.这是另一个。

private static boolean palindrome(String s){
    String revS = "";
    String checkS = s.toLowerCase();
    String[] checkSArr = checkS.split("");

    for(String e : checkSArr){
        revS = e + revS;
    }

    return (checkS.equals(revS)) ? true : false;
}
  • This implementation works for numbers and strings.此实现适用于数字和字符串。
  • Since we are not writing anything, so there is no need to convert the string into the character array.由于我们没有写任何东西,所以不需要将字符串转换为字符数组。
public static boolean isPalindrome(Object obj)
{
    String s = String.valueOf(obj);

    for(int left=0, right=s.length()-1; left < right; left++,right--)
    {
        if(s.charAt(left++) != s.charAt(right--))
            return false;
    }
    return true;
}

Why not just :为什么不只是:

boolean isPalindrom(String s) {
        char[] myChars = s.toCharArray();
        for (int i = 0; i < myChars.length/2; i++) {
            if (myChars[i] != myChars[myChars.length - 1 - i]) {
                return false;
            }
        }
        return true;
}
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;
                }

            }
        }
    }

}
private static boolean isPalindrome(String word) {

        int z = word.length();
        boolean isPalindrome = false;

        for (int i = 0; i <= word.length() / 2; i++) {
            if (word.charAt(i) == word.charAt(--z)) {
                isPalindrome = true;
            }
        }

        return isPalindrome;
    }

I was looking for a solution that not only worked for palindromes like...我正在寻找一种不仅适用于回文的解决方案,例如......

  • "Kayak" “皮艇”
  • "Madam" “女士”

...but as well for... ……但也为了……

  • "A man, a plan, a canal, Panama!" “一个人,一个计划,一条运河,巴拿马!”
  • "Was it a car or a cat I saw?" “我看到的是汽车还是猫?”
  • "No 'x' in Nixon" “尼克松没有‘x’”

Iterative : This has be proven as a good solution.迭代这已被证明是一个很好的解决方案。

private boolean isPalindromeIterative(final String string)
    {
        final char[] characters =
            string.replaceAll("[\\W]", "").toLowerCase().toCharArray();

        int iteratorLeft = 0;
        int iteratorEnd = characters.length - 1;

        while (iteratorEnd > iteratorLeft)
        {
            if (characters[iteratorLeft++] != characters[iteratorEnd--])
            {
                return false;
            }
        }

        return true;
    }

Recursive .递归的 I think this solution shouldn't be much worse than the iterative one.我认为这个解决方案不应该比迭代解决方案差太多。 Is a little bit crapy we need to extract the cleaning step out of the method to avoid unnecesary procesing.我们需要从方法中提取清洁步骤以避免不必要的处理,这有点蹩脚。

private boolean isPalindromeRecursive(final String string)
        {
            final String cleanString = string.replaceAll("[\\W]", "").toLowerCase();
            return isPalindromeRecursiveRecursion(cleanString);
        }

private boolean isPalindromeRecursiveRecursion(final String cleanString)
        {
            final int cleanStringLength = cleanString.length();

            return cleanStringLength <= 1 || cleanString.charAt(0) ==
                       cleanString.charAt(cleanStringLength - 1) &&
                       isPalindromeRecursiveRecursion  
                           (cleanString.substring(1, cleanStringLength - 1));
        }

Reversing : This has been proved as a expensive solution.倒车这已被证明是一种昂贵的解决方案。

private boolean isPalindromeReversing(final String string)
    {
        final String cleanString = string.replaceAll("[\\W]", "").toLowerCase();
        return cleanString.equals(new StringBuilder(cleanString).reverse().toString());
    }

All the credits to the guys answering in this post and bringing light to the topic.所有在这篇文章中回答并为该主题带来亮点的人的所有功劳。

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"));
}

Here you can check palindrome a number of String dynamically在这里你可以动态检查回文数个String

import java.util.Scanner;

public class Checkpalindrome {
 public static void main(String args[]) {
  String original, reverse = "";
  Scanner in = new Scanner(System.in);
  System.out.println("Enter How Many number of Input you want : ");
  int numOfInt = in.nextInt();
  original = in.nextLine();
do {
  if (numOfInt == 0) {
    System.out.println("Your Input Conplete");
   } 
  else {
    System.out.println("Enter a string to check palindrome");
    original = in.nextLine();

    StringBuffer buffer = new StringBuffer(original);
    reverse = buffer.reverse().toString();

  if (original.equalsIgnoreCase(reverse)) {
    System.out.println("The entered string is Palindrome:"+reverse);
   } 
  else {
    System.out.println("The entered string is not Palindrome:"+reverse);
    }
 }
   numOfInt--;
    } while (numOfInt >= 0);
}
}

IMO, the recursive way is the simplest and clearest. IMO,递归方式是最简单明了的。

public static boolean isPal(String s)
{   
    if(s.length() == 0 || s.length() == 1)
        return true; 
    if(s.charAt(0) == s.charAt(s.length()-1))
       return isPal(s.substring(1, s.length()-1));                
   return false;
}

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)));
    }
}

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");
     }
}

在此输入图像描述

import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class GetAllPalindromes 
{
    static Scanner in;

    public static void main(String[] args) 
    {
        in = new Scanner(System.in);
        System.out.println("Enter a string \n");
        String abc = in.nextLine();
        Set a = printAllPalindromes(abc);
        System.out.println("set is   " + a);
    }

    public static Set<CharSequence> printAllPalindromes(String input) 
    {
        if (input.length() <= 2) {
            return Collections.emptySet();
        }

        Set<CharSequence> out = new HashSet<CharSequence>();
        int length = input.length();

        for (int i = 1; i < length - 1; i++) 
        {
            for (int j = i - 1, k = i + 1; j >= 0 && k < length; j--, k++) 
            {
                if (input.charAt(j) == input.charAt(k)) {
                    out.add(input.subSequence(j, k + 1));
                } else {
                    break;
                }
            }
        }
        return out;
    }
}

**Get All Palindrome in s given string**

Output D:\\Java>java GetAllPalindromes Enter a string 输出 D:\\ Java> java GetAllPalindromes输入字符串

Hello user nitin is my best friend wow ! 你好用户nitin是我最好的朋友哇!

Answer is set is [nitin, nitin , wow , wow, iti] 答案是 [nitin,nitin,哇,哇,iti]

D:\\Java> d:\\的Java>

For-loop contains sub.length() / 2 - 1 . For 循环包含sub.length() / 2 - 1 It has to be subtracted with 1 as the element in the middle of the string does not have to checked.它必须减去 1,因为不必检查字符串中间的元素。

For example, if we have to check an string with 7 chars (1234567), then 7/2 => 3 and then we subtrack 1, and so the positions in the string will become (0123456).例如,如果我们必须检查一个有 7 个字符 (1234567) 的字符串,那么 7/2 => 3 然后我们子跟踪 1,因此字符串中的位置将变为 (0123456)。 The chars checked with be the 0, 1, 2 element with the 6, 5, 4 respectively.检查的字符分别是 0、1、2 元素和 6、5、4。 We do not care about the element at the position 3 as it is in the exact middle of the string.我们不关心位置 3 处的元素,因为它正好位于字符串的中间。

 private boolean isPalindromic(String sub) {
        for (int i = 0; i <= sub.length() / 2 - 1; i++) {
            if (sub.charAt(i) != sub.charAt(sub.length() - 1 - i)) {
                return false;
            }
        }
        return true;
    }
package basicprogm;

public class pallindrome {

  public static void main(String[] args) {
    // TODO Auto-generated method stub

    String s= "madam" ;
    //to store the values that we got in loop
    String t="";
    for(int i=s.length()-1;i>=0;i--){
      t=t+s.charAt(i);
    }
    System.out.println("reversed word is "+ t);

    if (t.matches(s)){
      System.out.println("pallindrome");
    }
    else{
      System.out.println("not pallindrome");
    }
  }
}
 public boolean isPalindrome(String input) {
    char[] inputChars = input.toCharArray();
    int inputLength = inputChars.length;
    int inputMid = inputLength / 2;

    for (int i = 0; i <= inputMid; i++) {
        if (inputChars[i] != inputChars[inputLength - i - 1]) {
             return false;
        } 
    }
    return true;
}

The method determines whether a string input is a palindrome.该方法确定字符串输入是否为回文。 In this method the loop iterates for half of the input length resulting in less performance concern and more concise application.在这种方法中,循环迭代输入长度的一半,从而减少性能问题和更简洁的应用程序。

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        char[] array = A.toCharArray();
        String str = "";
        for(int i=A.length()-1;i>=0;i--){
            str = str + array[i];
        }
        if(A.equalsIgnoreCase(str))System.out.println("Yes");
        else System.out.println("No");
    }
}

In PHP在 PHP 中

function isPalindrome($string) {
    return (strrev($string) == $string) ? true : false;
}

var_dump(isPalindrome('madam')); //bool(true)
var_dump(isPalindrome('dell')); //bool(false)
var_dump(isPalindrome('1221')); //bool(true)
/**
 * Check whether a word is a palindrome
 *
 * @param word the word
 * @param low  low index
 * @param high high index
 * @return {@code true} if the word is a palindrome;
 * {@code false} otherwise
 */
private static boolean isPalindrome(char[] word, int low, int high) {
    if (low >= high) {
        return true;
    } else if (word[low] != word[high]) {
        return false;
    } else {
        return isPalindrome(word, low + 1, high - 1);
    }
}

/**
 * Check whether a word is a palindrome
 *
 * @param the word
 * @return {@code true} if the word is a palindrome;
 * @code false} otherwise
 */
private static boolean isPalindrome(char[] word) {
    int length = word.length;
    for (int i = 0; i <= length / 2; i++) {
        if (word[i] != word[length - 1 - i]) {
            return false;
        }
    }
    return true;
}

public static void main(String[] args) {
    char[] word = {'a', 'b', 'c', 'b', 'a' };
    System.out.println(isPalindrome(word, 0, word.length - 1));
    System.out.println(isPalindrome(word));
}

Using Stream API:使用流 API:

private static boolean isPalindrome(char[] warray) {
    return IntStream.range(0, warray.length - 1)
            .takeWhile(i -> i < warray.length / 2)
            .noneMatch(i -> warray[i] != warray[warray.length - 1 - i]);
}
public class palindrome {

    public static void main(String[] args) {

        Scanner scanner=new Scanner(System.in);
        System.out.println("Enter the line you want to check palindrome:");
        String s= scanner.nextLine();

        StringTokenizer separate = new StringTokenizer(s, " ");
        System.out.println("\nPalindrome Words are: ");
        while(separate.hasMoreTokens()) {
            String word = separate.nextToken();
            String reversedWord = new StringBuilder(word).reverse().toString().toLowerCase();
            if ((word.toLowerCase().equals(reversedWord))){
                System.out.println(word);
            }
        }
    }
}

We can reduce the loop to half of the length:我们可以将循环减少到长度的一半:

function isPallindrome(s) {
  let word= s.toLowerCase();
  let length = word.length -1;
  let isPallindrome= true;
  for(let i=0; i< length/2 ;i++){
    if(word[i] !== word[length -i]){
      isPallindrome= false;
      break;
    }
  }
  return isPallindrome;
}

I created a new time complexitey Java solution 7 ms, and very easy!我创建了一个新的时间复杂度 Java 解决方案 7 毫秒,非常简单! I take the desired numbers and letters from the ASCII table, do not touch the rest, and finally return true or false, checking that the two strings are equal to each other.我从ASCII表中取出想要的数字和字母,其余的不碰,最后返回true或false,检查两个字符串是否相等。

StringBuilder newString = new StringBuilder();

    s = s.toLowerCase();

    for (char ch : s.toCharArray()) {

        if (97 <= (int) ch && (int) ch <= 122 || 48 <= (int)ch && (int)ch <= 57) {

            newString.append(ch);
        }
    }

    String nextS = new StringBuilder(newString).reverse().toString();
    
    return nextS.equals(newString.toString());

Besides the way using StringBuilder / StringBuffer to reverse the string and check it still equals the original one, here's a way by for loop.除了使用StringBuilder / StringBuffer来反转字符串并检查它仍然等于原始字符串的方式之外,这里还有一种for循环的方式。

private boolean isPalindrome(String word) {
    if (word == null || word.isEmpty()) {
        return false;
    }

    int length = word.length();
    int middleIndex = length / 2;

    for (int index = 0; index < middleIndex; index++) {
        if (word.charAt(index) != word.charAt(length - index - 1)) {
            return false;
        }
    }

    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM