你如何检查一个单词是否有回文变位



如何将回文单词与变位符中新形成的单词进行比较?

你如何抓住一个新形成的单词,将其与输入单词进行比较?

这是我的代码:

public class SampleCode2 {
public static boolean isPalindromic(String word, String mark) {
if (word.length() == 0) {
}
for (int i = 0; i < word.length(); i++) {
String newMark = mark + word.charAt(i);
String newLetters = word.substring(0, i) +
word.substring(i + 1);
}
String ifPalindrome = ""; //will store here the reversed string
String original = word; //will store here the original input word
//to reverse the string
for (int i = word.length() - 1; i >= 0; i--) {
ifPalindrome += word.charAt(i);
}
//to compare the reversed string to the anagram
if (word.equals(ifPalindrome)) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
boolean check = isPalindromic("mmaad", "");
System.out.println(check);
}
}

它还没有完成,因为排列和比较不起作用。输出显示false,我需要它是true,因为MMAAD的变位符是madam。我必须检查madam是否真的是mmaad的回文。

所以我所做的是使用HashMap,而不是从给定的word创建words
字符串可以是evenodd长度


如果";EVEN";字符串是回文的,然后每";字符";在String中将出现even
例如:字符串str=maam:m=2,a=2


如果";ODD";字符串是回文,则只有1个字符出现odd,其余字符将出现even
例如:字符串str=mmaad:m=2,a=2,d=1


要存储字符串中字符的出现,我们将使用HashMap,其中字符串的字符是KEY,其出现是VALUE

HashMap<Character,Integer> mapChar = new HashMap<>();

我们将把HashMap中的每个字符与它在字符串中出现的次数相加。

现在我们将检查CCD_;甚至";或";奇数";如果";EVEN";长度字符串我们知道每个字符将出现CCD_ 19次;ODD";我们返回false的次数,即它不是回文

for (Map.Entry<Character, Integer> entries : mapChar.entrySet()) {
if (entries.getValue() % 2 != 0) {
return false;
}
}

如果";ODD";长度字符串我们知道只有一个字符会出现odd次,其余的都是EVEN
如果有两个字符出现odd次,那么它不是回文

// Number of times odd value Character as occurred
int occur1 = 0;
for (Map.Entry<Character, Integer> entries : mapChar.entrySet()) {
if (entries.getValue() % 2 == 1) {
occur1++;
if (occur1 > 1) {
return false;
}
}
}

以下是整个代码:

public static void main(String[] args) throws Exception {
boolean check = isPalindromic("malayalam", "");
System.out.println(check);
}
public static boolean isPalindromic(String word, String mark) {
boolean isPal = true;
if (word.length() == 0) {
return false;
}
HashMap<Character, Integer> mapChar = new HashMap<>();
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (mapChar.containsKey(ch)) {
mapChar.put(ch, mapChar.get(ch) + 1);
} else {
mapChar.put(ch, 1);
}
}
if (word.length() % 2 == 0) {
for (Map.Entry<Character, Integer> entries : mapChar.entrySet()) {
if (entries.getValue() % 2 != 0) {
return false;
}
}
} else {
int occur1 = 0;
for (Map.Entry<Character, Integer> entries : mapChar.entrySet()) {
if (entries.getValue() % 2 == 1) {
occur1++;
if (occur1 > 1) {
isPal = false;
break;
}
}
}
}
return isPal;
}

输出:

mmaa
Is Palindrome: true
mmaad
Is Palindrome: true
niti
Is Palindrome: false

如果你想找到一个回文单词的变位符列表,你可以将此任务分为两部分:首先获得该单词List<String>的字符排列列表,即变位符列表,然后过滤那些回文字符串。例如,对于mmaad字符串,回文为:

madam
amdma

对于大型字符串来说,获取排列列表是一项昂贵的操作,因为排列的数量是字符串长度阶乘。例如,对于mmaad字符串,有120个排列。之后过滤回文比较便宜。

在线试用!

public static void main(String[] args) {
// list of distinct permutations
getPermutations("mmaad")
// Stream<String>
.stream()
// filter palindromes
.filter(str -> isPalindrome(str))
// output
.forEach(System.out::println);
}
/**
* @param str source string, may contain surrogate pairs.
* @return whether the source string is a palindrome.
*/
public static boolean isPalindrome(String str) {
// array of characters of the string
int[] chars = str.codePoints().toArray();
return IntStream
// iterate from the beginning to the middle of the string
.range(0, chars.length / 2)
// compare the characters: first - last, second - penultimate
.allMatch(i -> chars[i] == chars[chars.length - i - 1]);
}
/**
* @param str source string, may contain surrogate pairs.
* @return a list of distinct permutations of characters of the source string.
*/
public static List<String> getPermutations(String str) {
// array of characters of the string
int[] chars = str.codePoints().toArray();
return IntStream.range(0, chars.length)
// Stream<List<Map<Integer,String>>>
.mapToObj(i -> IntStream.range(0, chars.length)
// represent each character as Map<Integer,String>
.mapToObj(j -> Map.of(j, Character.toString(chars[j])))
// collect a list of maps
.collect(Collectors.toList()))
// reduce a stream of lists to a single list
.reduce((list1, list2) -> list1.stream()
// summation of pairs of elements,
// i.e. maps, from two lists
.flatMap(map1 -> list2.stream()
// filter out those keys
// that are already present
.filter(map2 -> map2.keySet().stream()
.noneMatch(map1::containsKey))
// join entries of two maps
.map(map2 -> {
Map<Integer, String> map =
new LinkedHashMap<>();
map.putAll(map1);
map.putAll(map2);
return map;
}))
// collect into a single list
.collect(Collectors.toList()))
// List<Map<Integer,String>>
.orElse(List.of(Map.of(0, str)))
// Stream<Map<Integer,String>>
.stream()
// map of strings to a single string
.map(map -> String.join("", map.values()))
.distinct()
// list of distinct permutations
.collect(Collectors.toList());
}

另请参阅:
•如何在不混合元组的情况下创建元组的所有排列?
•反向字符串打印方法

最新更新