如何检查is_palindrome的字符串列表[关闭]

问题描述 投票:-4回答:1

我如何编码它遍历列表并生成TRUE FALSE TRUE

randomphrase = ['lanal', 'samuel is my cat', 'too hot to hoot']
print(is_palindrome(randomphrase))
string list split palindrome divide
1个回答
0
投票
import java.util.ArrayList; import java.util.Arrays; public class StackOverflow { public static void main(String args[]) { ArrayList<String> randomPhrases = new ArrayList<>(Arrays.asList("lanal", "samuel is a cat", "too hot to hoot")); // I loop through the list and check if each string is a palindrome for (String str : randomPhrases) { isPalindrome(str); } } private static boolean isPalindrome(String str) { //test for end of recursion if (str.length() < 2) { System.out.println("true"); return true; } //check first and last character for equality if (str.charAt(0) != str.charAt(str.length() - 1)) { System.out.println("false"); return false; } //recursion call return isPalindrome(str.substring(1, str.length() - 1)); } }
© www.soinside.com 2019 - 2024. All rights reserved.