Open
Conversation
Author
|
can you check it and merge it @NirmalSilwal ? |
Owner
|
Hi @ifenil, could you add another approach or more optimized solution. That would be better PR if you are considering hactoberfest. |
Author
|
@NirmalSilwal , |
|
The alternative solution public class Solution {
public boolean isPalindrome(String s) {
int i = 0;
int j = s.length() - 1;
boolean res = true;
while (res) {
// Iterates through string to find first char which is alphanumeric.
// Done to ignore non-alphanumeric characters.
// Starts from 0 to j-1.
while (i < j && isNotAlphaNumeric(s.charAt(i))) {
i++;
}
// Similarly from j-1 to 0.
while (i < j && isNotAlphaNumeric(s.charAt(j))) {
j--;
}
// Checks if i is greater than or equal to j.
// The main loop only needs to loop n / 2 times hence this condition (where n is string
// length).
if (i >= j) {
break;
}
// Assigning found indices to variables.
// The upperToLower function is used to convert characters, if upper case, to lower
// case.
// If already lower case, it'll return as it is.
char left = upperToLower(s.charAt(i));
char right = upperToLower(s.charAt(j));
// If both variables are not same, result becomes false, and breaks out of the loop at
// next iteration.
if (left != right) {
res = false;
}
i++;
j--;
}
return res;
}
private boolean isNotAlphaNumeric(char c) {
return (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && (c < '0' || c > '9');
}
private boolean isUpper(char c) {
return c >= 'A' && c <= 'Z';
}
private char upperToLower(char c) {
if (isUpper(c)) {
c = (char) (c + 32);
}
return c;
}
} |
vikasganiga05
suggested changes
Apr 26, 2022
vikasganiga05
left a comment
There was a problem hiding this comment.
My solution to the problem. Missing some edge cases(which can be added later).
public class Palindrome {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter the string: ");
String str = input.nextLine();
input.close();
int i = 0;
int j = str.length() - 1;
while (i < j) {
if (str.charAt(i) == str.charAt(j)) {
i++;
j--;
} else {
System.out.printf("The string %s is not a Palindrome. \n", str);
return;
}
}
System.out.printf("The string %s is a Palindrome. \n", str);
}
}
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Valid Palindeome From Leetcode August Challange ;
explanation of code line by line through comment;
easily understandable and simple code
it passed all the test cases;