Skip to content

Update Palindrome.java #665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 24, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update Palindrome.java
  • Loading branch information
yanglbme authored Dec 24, 2018
commit 2d383bf48e7f058ec6d20b668e7a89edb3d51d43
45 changes: 20 additions & 25 deletions Others/Palindrome.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
class Palindrome {

private String reverseString(String x){ //*helper method
String output = "";
for(int i=x.length()-1; i>=0; i--){
output += x.charAt(i); //addition of chars create String
}
return output;
}


public Boolean FirstWay(String x){ //*palindrome method, returns true if palindrome
if(x == null || x.length() <= 1)
return true;
return (x.equalsIgnoreCase(reverseString(x)));
}

public boolean SecondWay(String x)
{
if (x.length() == 0 || x.length() == 1)
return true;

if (x.charAt(0) != x.charAt(x.length() - 1))
return false;
private String reverseString(String x) { // *helper method
StringBuilder output = new StringBuilder(x);
return output.reverse().toString();
}

return SecondWay(x.substring(1 , x.length() - 1));
}
}
public boolean FirstWay(String x) { // *palindrome method, returns true if palindrome
if (x == null || x.length() <= 1)
return true;
return x.equalsIgnoreCase(reverseString(x));
}

public boolean SecondWay(String x) {
if (x.length() == 0 || x.length() == 1)
return true;

if (x.charAt(0) != x.charAt(x.length() - 1))
return false;

return SecondWay(x.substring(1, x.length() - 1));
}
}