-
Notifications
You must be signed in to change notification settings - Fork 0
LeetCode5 Solved #49
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
base: master
Are you sure you want to change the base?
LeetCode5 Solved #49
Conversation
| if(i+3 <= s.length() && isPalindrome(s.substring(i,i+3))) { | ||
| palindromeList.add(new Position_LeetCode5(i, i+3)); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got what you were trying to do here.
You put all segments with length 2 or length 3 into the list, and then you can enumerate all palindromes with length 2+2k and 3+2k. In this case you can enumerate all lengths except for 1, but we can ignore 1 for sure.
However, I don't think your algorithm runs in O(N).
Imagine a case with a string input "aaaa........................................aa", you basically enumerate all the O(N^2) of possibilities, where N equals to the length of the string.
Please try:
- Carefully think about why your algorithm is O(N^2) instead of O(N).
- Try to create some test cases which make your code run slowly. (this is very important if you have an interview in big techs like Google) you can watch this
- In order to solve this problem in O(N), you'll need an algorithm called "Manacher's Algorithm". pseudocode in wikipedia
Even more:
Manacher's algorithm is an extended version of Z-algorithm. You can also try to learn the Z-algorithm. (I've never implemented this before, but it might be useful someday)
| int strLen = str.length(); | ||
|
|
||
| // 각 위치에서 회문을 만들어주는 가장 큰 반지름을 저장하기 위한 배열 생성 | ||
| int strArr[] = new int[strLen]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll recommend you to give this array a more meaningful name.
The name should clearly indicate that the value means the radius of a given position.
| // 이미 입력된 반지름이 있으면 거기부터 시작 | ||
| int radius = strArr[center]; | ||
|
|
||
| while(center >= radius + 1 && // 왼쪽으로 중심에서 반지름까지가 0보다 크거나 같을 때 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It took me a while to understand what center >= radius + 1 means.
I'll recommend you to calculate the "position" in this case.
The center here means the position of the center, so the position of the left side should be center - radius.
Hence, the next coordinate you're trying should be center - radius - 1.
I think writing center - radius - 1 >= 0 gives us a better semantic to what you're going for here because center - radius - 1 and 0 are both coordinates.
| newCenter++; | ||
| } | ||
|
|
||
| center++; // 다음 글자에서 다시 측정 ( 뒤에 추가로 오는 글자를 포함하여 또 회문일수 있으므로 재측정 ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 41 to line 59 is wrong. Please check the algorithm again.
You can try some examples on paper and see why this part is wrong.
Time complexity : O(N)