Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Valid Palindrome#2

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ifenil wants to merge2 commits intoNirmalSilwal:master
base:master
Choose a base branch
Loading
fromifenil:master
Open

Conversation

ifenil
Copy link

@ifenilifenil commentedOct 3, 2020
edited
Loading

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;

@ifenil
Copy link
Author

can you check it and merge it@NirmalSilwal ?

@NirmalSilwal
Copy link
Owner

Hi@ifenil, could you add another approach or more optimized solution. That would be better PR if you are considering hactoberfest.
What do you think?

ifenil reacted with eyes emoji

@ifenil
Copy link
Author

ifenil commentedOct 3, 2020
edited
Loading

@NirmalSilwal ,
I tried but I couldn't find optimized solution.
I think it's easy to understandable for beginners like me.

@javadev
Copy link

The alternative solution

publicclassSolution {publicbooleanisPalindrome(Strings) {inti =0;intj =s.length() -1;booleanres =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.charleft =upperToLower(s.charAt(i));charright =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--;        }returnres;    }privatebooleanisNotAlphaNumeric(charc) {return (c <'a' ||c >'z') && (c <'A' ||c >'Z') && (c <'0' ||c >'9');    }privatebooleanisUpper(charc) {returnc >='A' &&c <='Z';    }privatecharupperToLower(charc) {if (isUpper(c)) {c = (char) (c +32);        }returnc;    }}

Copy link

@vikasganiga05vikasganiga05 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

My solution to the problem. Missing some edge cases(which can be added later).

publicclassPalindrome {publicstaticvoidmain(String[]args) {Scannerinput =newScanner(System.in);System.out.print("Please enter the string: ");Stringstr =input.nextLine();input.close();inti =0;intj =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);  }}

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@vikasganiga05vikasganiga05vikasganiga05 requested changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@ifenil@NirmalSilwal@javadev@vikasganiga05

[8]ページ先頭

©2009-2025 Movatter.jp