Skip to content

Commit 93c4822

Browse files
committed
Palindrome Creator
1 parent 96c0cd4 commit 93c4822

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

src/easy/PalindromeCreator.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package easy;
2+
3+
/**
4+
* Have the function PalindromeCreator(str) take the str parameter being passed
5+
* and determine if it is possible to create a palindromic string
6+
* of minimum length 3 characters by removing 1 or 2 characters.
7+
* ---
8+
* For example: if str is "abjchba" then you can remove the characters jc to produce "abhba"
9+
* which is a palindrome. For this example your program should return the two characters
10+
* that were removed with no delimiter and in the order they appear in the string, so jc.
11+
* ---
12+
* If 1 or 2 characters cannot be removed to produce a palindrome,
13+
* then return the string not possible. If the input string is already a palindrome,
14+
* your program should return the string palindrome.
15+
* ---
16+
* The input will only contain lowercase alphabetic characters.
17+
* Your program should always attempt to create the longest palindromic substring
18+
* by removing 1 or 2 characters (see second sample test case as an example).
19+
* The 2 characters you remove do not have to be adjacent in the string.
20+
*/
21+
public class PalindromeCreator {
22+
23+
/**
24+
* A support function checking if a given string is a palindrome.
25+
*
26+
* @param str input string
27+
* @return true if the string is a palindrome
28+
*/
29+
private static boolean isPalindrome(String str) {
30+
char[] strArr = str.toCharArray();
31+
int len = strArr.length;
32+
for (int i = 0; i < len / 2; i++) {
33+
if (strArr[i] != strArr[len - i - 1]) {
34+
return false;
35+
}
36+
}
37+
return true;
38+
}
39+
40+
/**
41+
* Palindrome Creator function.
42+
*
43+
* @param str input string
44+
* @return characters to be removed or "palindrome" or "not possible"
45+
*/
46+
private static String palindromeCreator(String str) {
47+
if (isPalindrome(str)) {
48+
return "palindrome";
49+
}
50+
51+
for (int i = 0; i < str.length(); i++) {
52+
StringBuilder combo = new StringBuilder();
53+
for (int k = 0; k < str.length(); k++) {
54+
if (k != i) {
55+
combo.append(str.charAt(k));
56+
}
57+
}
58+
if (isPalindrome(combo.toString()) && combo.length() >= 3) {
59+
return String.valueOf(str.charAt(i));
60+
}
61+
}
62+
63+
for (int i = 0; i < str.length(); i++) {
64+
for (int j = i; j < str.length(); j++) {
65+
StringBuilder combo = new StringBuilder();
66+
for (int k = 0; k < str.length(); k++) {
67+
if (k != i && k != j) {
68+
combo.append(str.charAt(k));
69+
}
70+
}
71+
if (isPalindrome(combo.toString()) && combo.length() >= 3) {
72+
return String.valueOf(str.charAt(i)) + str.charAt(j);
73+
}
74+
}
75+
}
76+
77+
return "not possible";
78+
}
79+
80+
/**
81+
* Entry point.
82+
*
83+
* @param args command line arguments
84+
*/
85+
public static void main(String[] args) {
86+
var result2 = palindromeCreator("racecar");
87+
System.out.println(result2);
88+
var result1 = palindromeCreator("vhhgghhgghhk");
89+
System.out.println(result1);
90+
var result3 = palindromeCreator("rats live on no evil stars");
91+
System.out.println(result3);
92+
}
93+
94+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy