Skip to content

Commit e6df53e

Browse files
committed
Palindrome Two
1 parent 3ff032c commit e6df53e

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/medium/PalindromeTwo.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package medium;
2+
3+
/**
4+
* Have the function PalindromeTwo(str) take the str parameter being passed
5+
* and return the string true if the parameter is a palindrome,
6+
* (the string is the same forward as it is backward)
7+
* otherwise return the string false.
8+
* ---
9+
* The parameter entered may have punctuation and symbols,
10+
* but they should not affect whether the string is in fact a palindrome.
11+
* For example: "Anne, I vote more cars race Rome-to-Vienna" should return true.
12+
*/
13+
public class PalindromeTwo {
14+
15+
/**
16+
* Check if a string is a palindrome.
17+
*
18+
* @param str input string
19+
* @return true if a string is a palindrome
20+
*/
21+
private static boolean isPalindrome(String str) {
22+
char[] strArr = str.toCharArray();
23+
int len = strArr.length;
24+
for (int i = 0; i < len / 2; i++) {
25+
if (strArr[i] != strArr[len - i - 1]) {
26+
return false;
27+
}
28+
}
29+
return true;
30+
}
31+
32+
/**
33+
* Palindrome Two function.
34+
*
35+
* @param str input string
36+
* @return "true" if a string is a palindrome
37+
*/
38+
public static String palindromeTwo(String str) {
39+
String parsed = str.toLowerCase().replaceAll("[^a-z$]", "");
40+
return isPalindrome(parsed) ? "true" : "false";
41+
}
42+
43+
/**
44+
* Entry point.
45+
*
46+
* @param args command line arguments
47+
*/
48+
public static void main(String[] args) {
49+
String result1 = palindromeTwo("Noel - sees Leon");
50+
System.out.println(result1);
51+
String result2 = palindromeTwo("A man, a plan, a canal – Panama.");
52+
System.out.println(result2);
53+
String result3 = palindromeTwo("No lemon, no melon!");
54+
System.out.println(result3);
55+
}
56+
57+
}

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