Created
August 29, 2016 19:33
-
-
Save w3cj/d08515f86f0dfa36d0f98bc0b86df6ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// O(n + n + n/2) | |
// O(2n + n/2) | |
// O(2.5n) | |
// O(n) | |
public boolean isPalindrome(String input) { | |
// replaceAll - n times | |
input = input.replaceAll("\\s",""); | |
// replaceAll - n times | |
int n = input.length(); | |
// for - n/2 times | |
for (int i = 0; i < (n/2); i ++){ | |
if (input.charAt(i) != input.charAt(n-i-1)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
// O(n + n + n + n + n) | |
// O(5n) | |
// O(n) | |
public boolean isPalindrome(String input) { | |
// replaceAll - n times | |
String word = input.replaceAll("\\s+",""); | |
// reverse - n times | |
// replaceAll - n times | |
// toString - n times | |
String reversedWord = new StringBuilder(input).reverse().toString().replaceAll("\\s+",""); | |
System.out.println(word); | |
System.out.println(reversedWord); | |
// equals - n times | |
if(word.equals(reversedWord)) { | |
return true; | |
} else { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment