-
Notifications
You must be signed in to change notification settings - Fork 17
/
Palindrome.java
124 lines (111 loc) · 3.91 KB
/
Palindrome.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package by.andd3dfx.string;
import java.util.HashSet;
import java.util.Set;
/**
* @see <a href="https://youtu.be/XZMOlvKRzd0">Video solution</a>
*/
public class Palindrome {
/**
* A palindrome is a word that reads the same backward or forward.
* Write a function that checks if a given word is a palindrome.
*/
public static boolean isPalindrome(String word) {
return isPalindrome(word, false);
}
/**
* A palindrome is a word that reads the same backward or forward. Write a function that checks if a given word is a
* palindrome. Character case should be ignored. For example, isPalindrome("Deleveled") should return true as
* a character case should be ignored, resulting in "deleveled", which is a palindrome since it reads the same
* backward and forward.
*/
public static boolean isPalindromeIgnoreCase(String word) {
return isPalindrome(word, true);
}
private static boolean isPalindrome(String word, boolean ignoreCase) {
if (word == null) {
return false;
}
if (ignoreCase) {
word = word.toLowerCase();
}
for (int i = 0; i < word.length() / 2; i++) {
if (word.charAt(i) != word.charAt(word.length() - 1 - i)) {
return false;
}
}
return true;
}
/**
* Remove palindromes from string.
*/
public static String removePalindromes(String string) {
String[] words = string.split(",?\\s");
for (String word : words) {
if (isPalindromeIgnoreCase(word)) {
string = string.replace(word, "");
}
}
return string
.replaceAll("(,\\s){2,}", ", ")
.replaceAll("\\s{2,}", " ")
.trim();
}
/**
* Remove Nth palindrome from string.
*/
public static String removeNthPalindrome(String string, int n) {
String[] words = string.split(",?\\s");
int counter = 0;
for (String word : words) {
if (isPalindromeIgnoreCase(word)) {
counter++;
if (counter == n) {
return string
.replace(word, "")
.replaceAll("(,\\s){2,}", ", ")
.replaceAll("\\s{2,}", " ")
.trim();
}
}
}
return string;
}
/**
* <pre>
* Given a string, find the longest substring which is palindrome.
* For example:
* Input: "forgeeksskeegfor", Output: "geeksskeeg"
* Input: "Geeks", Output: "ee""
* </pre>
*/
public static String longestPalindromeSubstring(String str) {
var result = str.substring(0, 1);
for (int i = 0; i < str.length(); i++) {
for (int j = str.length() - 1; j >= i + result.length() + 1; j--) {
var currentString = str.substring(i, j);
if (currentString.length() > result.length() && isPalindrome(currentString)) {
result = currentString;
}
}
}
return result;
}
/**
* Дана строка. Написать метод, проверяющий, можно ли перестановкой букв данной строки сформировать палиндром.
*/
public static boolean canFormPalindrome(String str) {
/**
* Идея решения в том, что в палиндроме все символы встречаются четное кол-во раз, кроме, максимум одного символа.
* Убеждаемся, что таких одиночных символов не более одного.
*/
Set set = new HashSet();
for (var ch : str.toCharArray()) {
if (!set.contains(ch)) {
set.add(ch);
} else {
set.remove(ch);
}
}
return set.size() <= 1;
}
}