We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The testcase 11 and 14 are not checking for both correct answer. I have verified it for test case 11. My answer is 8, while that in output file is 44. Both are correct as the indexes are wc and cw being compared and any of c and w can be removed.
The string is:
hgygsvlfcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcwflvsgygh
Thanx for idea... the improved method body:
int l = s.length();
int i,j,a,b;
for (i=0, j=l-1; ij) return -1;
if (s.charAt(i + 1) == s.charAt(j)) {
for (a = i + 1, b = j; a < b; a++, b--) {
if (s.charAt(a) != s.charAt(b))
return -1;
}
return i;
} else if (s.charAt(i) == s.charAt(j - 1)) {
for (a = i, b = j - 1; a < b; a++, b--) {
if (s.charAt(a) != s.charAt(b))
return -1;
}
return j;
}
return -1;
I guess there is no test case in hackerrank where a string after removing a character cannot become a palidrome. If there was, the above logic would fail.
If you remove 8th char it is not a palindrome anymore.
hgygsvlf (*wnsw) tuhmyaljkqlqjjqlqkjlaymhutws (nwcw) flvsgygh see those inside the bracket above. wnsw-nwcw.
whereas, if you remove 44th char, (i.e.) w, then: hgygsvlf (cwns) wtuhmyaljkqlqjjqlqkjlaymhutw (snwc) flvsgygh it becomes pallindrome. Hope this helps.
How is it ensured that the string is palindrome just by checking if first and last are same and second and second last are same ?, i mean we have to check the whole string. I am talking about the "if and else if condition".
That was a good example that threw the wrench in my program. If I hadn't seen the example, I could have not figured it out where am I going wrong... The guy who added this special case is really good.
It turns out that you still have to check the remaining string for symmetry. Optimizing to a single- or double-character look ahead turns out to break the solution. A good bit trickier than the grading would lead you to believe. It surely helps to make up your own test data to check for corner cases.
That's a good point. The second while-loop runs only if the first encountered left-right character pair aren't the same. It implicitly assumes that if removing the left character doesn't make a palindrome, then removing the right character must make it a palindrome.
In 'bugdeb', for instance, you break out of the first while loop with L=u, R=e. L gets incremented and points to g. The second while loop then compares g-e, sets 'Leftfault' to 0 and the print statement will trigger. The print statement says that removing the 'e' will make 'bugdeb' a palindrome, which is clearly not true.
The problem asks us to remove a 'single' character after which the string will be a plaindrome. The string 'bugdeb' cannot be considered as a test case because removal of any (1) character will not make it a plaindrome.
Is enought whit check the next char, I mean, if delete the i (index left to right) verify i+1 == j and i+2 == j-1, in case of delete j (index right to left) verify i == j-1 and i+1 == j-2.
Is't required verify whit a 2nd loop.
This will pass the testcases given, but is not a correct solution to the problem. For example, deleting index 0 of "bababcbcbababa" will not solve it, but it will pass your two-pass lookahead test ("ab" = "ba"), and even a three-pass lookahead ("aba" = "aba"). You must fully check that the string sans the character you deleted is a palindrome. Our saving grace is that the answer must be one of the two indexes involved in the mismatch we found, so if our initial guess is wrong then there's only one other possibility.
There's a note in the problem description, which states that the input string will be solvable or it will be a palindrome by default. Your string isn't correct by this definition.
Thank you for this hint! it really helped me in figuring out the bug with problems 11 & 14 :)
BTW, thsi problem looked easy at a first glance ... but it was kind of tricky after all!
it mentioned in the question there is always a possible answere, but in case you want to do it your satisfaction , you can try some if-else condition and print some message. but will not be related to problem
Actually the test case #11 and #14 should be fine. If you change the order of check logic, you may fail another 2 test cases. I think the problem lies on "checking s[j+1] & s[k], s[j]&s[k-1] is not sufficient". Try checking s[j+2] & s[k-1] also. I'm able to pass all test cases :)
44 is the only correct answer. If you remove char 8, the resultant string is not palindromic. You're only checking one character deep after finding the end of the initial palindrome.
You need to look one character beyond that. On the right side (going from right to left), the characters are wcwns.... On the left side (going from left to right), the characters are cwns.... You have to remove only w, because removing c will not result in a palindrome.
I guess I know why. Your algorithm is probably deleting the C(index 8) instead of w (index 44)
Compare the original string with its reversed form:
ori: hgygsvlfcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcwflvsgygh
rev: hgygsvlfwcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcflvsgygh
the difference appears at index 8. but the correct action is to remove index 44. If you remove 8 you wont have a palindrome.
Please test your code with the follwoing test case : bugdeb
Your code returns an index 4. but once we remove 'e', it is still not a palindrome. You should fix that.
the s.charAt() function accepts only int values in the brackets...so mayb if the length of the string is more than the int range, which is the case in 11 and 14..and hence mayb showing wrong answer
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int i,j;
i=0;
j=s.length()-1;
int flag=0;
int ind=0;
int d=0;
while(i
int q;
cin >> q;
for(int a0 = 0; a0 < q; a0++){
string s;
cin >> s;
int i;
for (i=0;i<s.length()/2;i++) {
if (s[i]==s[s.length()-1-i]) continue;
// check if substr is palindrome by getting rid of ith char
int j;
for (j=i+1;j<i+1+(s.length()-1-i-i)/2;j++) {
if (s[j]==s[s.length()-j]) continue;
cout << s.length()-1-i << endl;
break;
}
if (j==i+1+(s.length()-1-i-i)/2) cout << i << endl;
break;
}
if (i==s.length()/2) cout << -1 << endl;
}
The above algo is wrong, if you input "avidefdxiva" it gives output as 7, but it should be -1 as by removing 7th index the resultant string ("avidefdiva") won't be a palindrome.
And why is builder = new StringBuilder(text); done each time. just restore the deleted char. If we create a new object then it is no better than using String itself.
The above algo is wrong, if you input "avixdefdiva" it gives output as 3, but it should be -1 as by removing 3rd index the resultant string ("avidefdiva") won't be a palindrome.
staticintpalindromeIndex(Strings){// Complete this functioninti,res=-1,n,temp;char[]charStr=s.toCharArray();n=s.length();for(i=0;i<n;i++){temp=charStr[i]-charStr[n-1-i];if(temp==0)continue;elseif((charStr[i+1]-charStr[n-1-i]==0)&&(charStr[i+2]==charStr[n-2-i])){res=i;break;}else{res=n-1-i;break;}}returnres;}
Yea these cases are bit tricky ones you need to backtrack to the postion you have earlier selected n choose different path(i.e if you choose 8 for deletion now select len-8) when characters are not matching.
The string: "hgygsvlfcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcwflvsgygh" is present in testcase #11, and also in testcase #1.
In #1, if the code returns 8, it's fine, but en #11, return of an 8 fails the test
I made another approach for this problem in Python 3:
* invert all the string and store in s_i
* Iterarate s removing postion from start to end and iterate s_i removing postion from end to start
* if s and s_i are equals, return postion
This solution pass in all test cases.
def palindromeIndex(s):
N = len(s)
N_i = N - 1
s_i = s[::-1]
if s == s_i:
return -1
for i in range(N):
test = s[:i] + s[i+1:]
test_i = s_i[:N_i-i] + s_i[N_i-i+1:]
if test == test_i:
return i
return -1
This question is little bit weird . You have to assume that a given string is a pallindrome or just off by a single character . So basically you have to run a loop from both starting and ending and check which characters dont match. Now you have to remove any one of the character and check if the string is pallindrome .If yes return the index of the character removed or else return the index of other character (the character which didnt match at the start) .Just look at this code and you will understand what i was taking about .(Its a java code btw )
int low = 0 ;
int high = s.length()-1 ;
while(low < high)
{
if(s.charAt(low) != s.charAt(high))
{
break;
}
low++;
high--;
}
if(low > high)
{
return -1;
}
int res1 = high ;
int res2 = low ;
while(low < high)
{
if(s.charAt(low+1) != s.charAt(high))
{
return res1 ;
}
low++;
high--;
}
return res2 ;
The problem of test case 11 and 14 is that you have to check two index ahead in both cases
hgygsvlfcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcwflvsgygh
mismatch at index 8 so if you remove c at index 8 just checking after c is w then it will fail cause it won't be palindrome.
Check you answer for sample case: cwccw
You'll get an better understanding.
You can check my code, it works....
` python3 code
def palindromeIndex(s):
n = len(s)
if ispalindrome(s):
return -1
k = 0
for i in range(n//2):
if s[-i-1]!=s[i]:
if s[-i-2]==s[i] or s[i+1]==s[-i-1]:
wrd = remove(s,n-i-1)
if ispalindrome(wrd):
return n-i-1
wrd = remove(s,i)
if ispalindrome(wrd):
return i
return -1
>
brother, if we remove 8th letter 'c' we get .....fwns...... front left and from right we get ......fwcwns.... . but if we remove 44 letter we get a palandrome.
i faced the same problem , so i sort to check the third letter after mismatch
if s[i]!=s[j]:
if s[i]==s[j-1] and s[i+1]==s[j-2]:
return j
else return i
no, removing any of both char won't work, it has to be only 'w'(idx 44) because..
the string is "...fcwn...nwcwf..." (dots are all palindrome only you can check), now after char 'f', 'c'(idx 8) and 'w' char are different, now if you remove 'c' here.. the string would be "...fwn...nwcwf..." which doesn't make the string palindrome, but if you remove w, the string will be "...fcwn...nwcf..." which is palindrome, that's why here answer should be 44, not 8
My first algorithm was also failing tests 11 and 14. But I realized it was a problem of the algorithm, not of the test.
If you remove 8th caracter from the string "hgygsvlfcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcwflvsgygh" it will not become a palindrome.
I have 'isolated' my algorithm problem here.
"cwnnwcw"
We have to remove 6th carachter to form a palindrome.
For anoyone still looking for an O(n) solution (2 pointers) that passes test cases 11 and 14, here's a working C# solution with comments:
the issue with 11 & 14 is that we could remove/skip either the left or right character when iterating through, but only 1 of them leads to a solution, so we have to check the next character as well to see if left or right is the right to remove/skip
publicstaticintpalindromeIndex(strings){intcharRemoved=-1;intleft=0;intright=s.Length-1;while(right>left){if(s[left]!=s[right]){if(charRemoved!=-1)//already removed a char, we're donereturn-1;boolremoveLeft=left<s.Length-1&&s[left+1]==s[right];boolremoveRight=right>0&&s[left]==s[right-1];if(removeLeft&&removeRight){//if we could remove either//check one more char furhter in...removeLeft=(left+1)<s.Length-1&&s[left+2]==s[right-1];removeRight=!removeLeft;}if(removeLeft){charRemoved=left;left++;//skip this character}elseif(removeRight){charRemoved=right;right--;//skip this character}else{//if removing neither character worksreturn-1;}}//move both pointers to the next character toward the middleright--;left++;}returncharRemoved;//return the index of the char we removed}
The answer is 44. I created an extra check palindrome function, and removing index 8 only does not result in a palindrome.
only by removing index 44, will the resulting string be a palindrome.
Palindrome Index
You are viewing a single comment's thread. Return to all comments →
The testcase 11 and 14 are not checking for both correct answer. I have verified it for test case 11. My answer is 8, while that in output file is 44. Both are correct as the indexes are wc and cw being compared and any of c and w can be removed. The string is: hgygsvlfcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcwflvsgygh
Same problem here, #11 and #14 are failing!
My python logic passed all test cases.
Hackerrank - Palindrome Index Solution
My Java Solution
This wont work though if the string cant be a palindrome after removing a character , right?
please explain your logic
pls explanation your code ??
Thanx for idea... the improved method body: int l = s.length(); int i,j,a,b; for (i=0, j=l-1; ij) return -1; if (s.charAt(i + 1) == s.charAt(j)) { for (a = i + 1, b = j; a < b; a++, b--) { if (s.charAt(a) != s.charAt(b)) return -1; } return i; } else if (s.charAt(i) == s.charAt(j - 1)) { for (a = i, b = j - 1; a < b; a++, b--) { if (s.charAt(a) != s.charAt(b)) return -1; } return j; } return -1;
This solution is really help me :)
Your java solution doesn't work.
for example : aabdefcaa should return -1 you return 6
both the python and the java solution you gave, did NOT pass MOST of the tests I had.
I guess there is no test case in hackerrank where a string after removing a character cannot become a palidrome. If there was, the above logic would fail.
same problem.pls help
same here...
If you remove 8th char it is not a palindrome anymore. hgygsvlf (*wnsw) tuhmyaljkqlqjjqlqkjlaymhutws (nwcw) flvsgygh see those inside the bracket above. wnsw-nwcw. whereas, if you remove 44th char, (i.e.) w, then: hgygsvlf (cwns) wtuhmyaljkqlqjjqlqkjlaymhutw (snwc) flvsgygh it becomes pallindrome. Hope this helps.
No the author is correct with all the test cases. check it out. Used Goto statement here but logic is correct.
yes same problem in test case 11 & 14
Why use 'goto' when you can use 'break'?
11th test case worked in my case-
int check_pal(string s1) {
int i,k,flag=0; for(i=0,k=s1.length()-1;i<(s1.length())/2 && k>=(s1.length())/2;i++,k--) {
} int checked_pal(int j,string s1) {int i,k; for(i=0,k=s1.length()-1;i<(s1.length())/2 && k>=(s1.length())/2;i++,k--) {
} int main() { int t,j,i; cin>>t; for(i=1;i<=t;i++) { string s1; cin>>s1; //cout<<"ok--"<<(s1.length()-1)<
}
Thanks, That helped me I was using the same logic only I missed "(z[a+2]==z[q-1-a-1])", I passed test case 11 and 14 too after this.
gorgeous solution
clear solution.
You should check is the string palindrome at starting of palindromeIndex as it will reduce all the computation.
Wonder how it passed all the test cases. Answer of aaaaabab should it -1. This code will give 7.
Readable solution but… time complexity is O()
I still confused with the checking part. I don't get how it is going to check all the possibles. 0 1 2 3 4 i from 0 1 j from 4 3
when index = 0, check from 1 to 4 index = 4, i > j loop is not going to loop. ?????
Therefore, can you explain this to me. Addtionally, Thank You for sharing reduced for loop. I use silmiar idea but I create my own checking function.
The code is not correct, even though it has passed all given test cases it is wrong.
EG: aabbtkbbaac your code gives output 10 but infact the answer is "-1"
the EG: aabbtkbbaac you have given is wrong, Pls read the question carefully.
Please excuse everything thats wrong with this style of writing code. I'm just an amatuer. But, i did get the right output for your String.
doesnt work with aacbb
it is very helpful...!!bro...!!!thanks a lot..!!
wrong solution! string='adcdecdba' *your code =7 * ans=-1
Yes..you are right.. Thank you very much..
How is it ensured that the string is palindrome just by checking if first and last are same and second and second last are same ?, i mean we have to check the whole string. I am talking about the "if and else if condition".
I think the same, u just check the next char too in both cases and it passes all cases..... Cheers
That was a good example that threw the wrench in my program. If I hadn't seen the example, I could have not figured it out where am I going wrong... The guy who added this special case is really good.
Simple approach
Thank you for this.
Case number 11 14 are also correct check my py code
The code is not correct, even though it has passed all given test cases it is wrong.
EG: aabbtkbbaac your code gives output 10 but infact the answer is "-1"
Dude,the question said that it is ensured that the removal of a character will make the string palindrome.
even after removing the 10th character the resulted string is still not palindrome.
original : aabbtkbbaac after deleting 10th character : new : aabbktbbaa
tk and kt are different.
Thats really nice approach to solve this. I was removing every alphabet and checking for palindrome Which resulted in time complexity
Hi just wondering where this
raw_input()
was defined?raw_input was a python 2 function superseded by just input in python 3.
complete solution https://www.hackerrank.com/challenges/palindrome-index/forum/comments/474736
Simple approach
Original string:
hgygsvlfcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcwflvsgygh
After deleting 8'th character:
hgygsvlfwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcwflvsgygh
Please check if it is a palindrome :)
it is not palindrome because check after f from front and back
C solution:
}
@etayluz. I didn't get why you've used the second while loop. Can you please explain?
Thanks a lot.
:)
It turns out that you still have to check the remaining string for symmetry. Optimizing to a single- or double-character look ahead turns out to break the solution. A good bit trickier than the grading would lead you to believe. It surely helps to make up your own test data to check for corner cases.
will it work for the string abc???
how to write code in comment
how to write code in comment
look for when commenting in the box. :|
copy - paste
how to write code in comment
enclose it within 3 grave accent(') three at beginning and three at last It is the key to the left of "1"
This solution is wrong.It doesnt take into account the cases in which either deletion on left or on right doesnt make it a palindrome.
That's a good point. The second while-loop runs only if the first encountered left-right character pair aren't the same. It implicitly assumes that if removing the left character doesn't make a palindrome, then removing the right character must make it a palindrome.
In 'bugdeb', for instance, you break out of the first while loop with L=u, R=e. L gets incremented and points to g. The second while loop then compares g-e, sets 'Leftfault' to 0 and the print statement will trigger. The print statement says that removing the 'e' will make 'bugdeb' a palindrome, which is clearly not true.
The problem asks us to remove a 'single' character after which the string will be a plaindrome. The string 'bugdeb' cannot be considered as a test case because removal of any (1) character will not make it a plaindrome.
Is enought whit check the next char, I mean, if delete the i (index left to right) verify i+1 == j and i+2 == j-1, in case of delete j (index right to left) verify i == j-1 and i+1 == j-2. Is't required verify whit a 2nd loop.
(Sorry for my english)
I dont understant your code can you please explain me
Thanks Man,
Your code clears the concept easily.
Amazing code
@abhiranjan Please look into test cases 11, 14. These are not accepting all possible answers.
Yeah, i have the same issue. It seems that test cases #11 and #14 do not correspond to problem statement. Or something is missed in problem statement.
Nope they are good. Please check your output.
You can download input from the submission page. Try your output after hardcoding it there.
Yeah, you are right! My bad.. (x__x) -> found a bug (now all 14 tests pass). Thanks for good challenge and for quick answer! ;-)
@garek, any hint possible(that will not spoil the challenge for me)?
In my case, i did not consider all possible cases while choosing which index to remove.
The abhiranjan`s comment with 'hgygsvlfcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcwflvsgygh' input was very helpful.
Hope, no spoiler here.
Try this code u will get the answer
int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */
char a[100007]; int size; cin>>size;
size--; } getch(); return 0; }
No need to add so many conditions. Only look at two char ahead. It will be reduces to two if conditions with two statements in each.
This will pass the testcases given, but is not a correct solution to the problem. For example, deleting index 0 of "bababcbcbababa" will not solve it, but it will pass your two-pass lookahead test ("ab" = "ba"), and even a three-pass lookahead ("aba" = "aba"). You must fully check that the string sans the character you deleted is a palindrome. Our saving grace is that the answer must be one of the two indexes involved in the mismatch we found, so if our initial guess is wrong then there's only one other possibility.
There's a note in the problem description, which states that the input string will be solvable or it will be a palindrome by default. Your string isn't correct by this definition.
It's solved by deleting the character at the final index, creating the string "bababcbcbabab".
Oh, yeah, now I can see that. Sorry)
in 11th test case after deleting the 8th letter in the word it still doesnt make a pallindrome :P
Here is my c++ solution : explanation here https://youtu.be/QjHpvMdfnqs,
Thank you for this hint! it really helped me in figuring out the bug with problems 11 & 14 :) BTW, thsi problem looked easy at a first glance ... but it was kind of tricky after all!
what was the bug u found there?
Here is my c++ solution : explanation here https://youtu.be/QjHpvMdfnqs,
Helpful !!
Thank you. Your comment prompted me to rewrite my solution as a recursive function - the problem turns out to be much easier to solve this way.
you gotta delete the 8th char from the end. the 'w'. hgygsvlfcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwc(w)flvsgygh
Same here failing on my submission; https://www.hackerrank.com/challenges/palindrome-index/submissions/code/12772467
You should check all symbols of a palindrome when you find the index of palindrome breaker.
same problem here
8 cannot be the ans..as string will still not be pallindrome after removing 'c'. correct ans is associated with the removal of 'w'.
is it working now ? m facing the same problem !
actually, both answers aren't correct... removing 8 "c", doesn't make the resulting string a palindrome.
what to print if the given string can not be converrted to palindrome????????
it mentioned in the question there is always a possible answere, but in case you want to do it your satisfaction , you can try some if-else condition and print some message. but will not be related to problem
Actually the test case #11 and #14 should be fine. If you change the order of check logic, you may fail another 2 test cases. I think the problem lies on "checking s[j+1] & s[k], s[j]&s[k-1] is not sufficient". Try checking s[j+2] & s[k-1] also. I'm able to pass all test cases :)
Try these 3 test cases:
abcba
cwwcw
wcwwc
int main() { int t; cin>>t; for(int i=0;i>str; long long int m; long long int k=strlen(str);
}
successfully passed 1st 10 test cases.not able to pass remaining 4 plzzz help
even checking j+2 & k-1 won't suffice (even if you pass all the testcases). try with this case:
abaabab
.the solution should be, try to remove s(i) and check if it becomes a palindrome.
44 is the only correct answer. If you remove char 8, the resultant string is not palindromic. You're only checking one character deep after finding the end of the initial palindrome.
Thought I was going crazy...
You need to look one character beyond that. On the right side (going from right to left), the characters are
wcwns...
. On the left side (going from left to right), the characters arecwns...
. You have to remove only w, because removing c will not result in a palindrome.thanx just added && (s.charAt(start+2) == s.charAt(end-1)) then all test cases passed
for string - "abcdefghzxqwgnbam", your code is returning 16, but it can never become a pelindrom and hence should return -1.
I guess I know why. Your algorithm is probably deleting the C(index 8) instead of w (index 44)
Compare the original string with its reversed form: ori: hgygsvlfcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcwflvsgygh rev: hgygsvlfwcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcflvsgygh
the difference appears at index 8. but the correct action is to remove index 44. If you remove 8 you wont have a palindrome.
Same. Issue. 11 & 14 not working
As you can see you also need to check the next two characters to see if the string will continue to be a palindrome
All test cases are correct, these are just tricky.
check this in java import java.io.; import java.util.;
public class Solution {
}
what about the case "abc"?
Please test your code with the follwoing test case : bugdeb Your code returns an index 4. but once we remove 'e', it is still not a palindrome. You should fix that.
I got mistake in my code from your code.Thanks
It is the same even after two years, it is not accepting either one of both the valid answers
the s.charAt() function accepts only int values in the brackets...so mayb if the length of the string is more than the int range, which is the case in 11 and 14..and hence mayb showing wrong answer
include
include
include
include
include
using namespace std;
int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t; cin>>t; while(t--) { string s; cin>>s; int i,j; i=0; j=s.length()-1; int flag=0; int ind=0; int d=0; while(i
}
The program is correct check my py code
why would you use an underscore if you're actually accessing the value.... very anti-pythonic
only w can be removed at 44th index
The above algo is wrong, if you input "avidefdxiva" it gives output as 7, but it should be -1 as by removing 7th index the resultant string ("avidefdiva") won't be a palindrome.
And why is builder = new StringBuilder(text); done each time. just restore the deleted char. If we create a new object then it is no better than using String itself.
TC11 and 14 are correct... here is my solution
Have a similar code, but missed the login in 'else' stametent. You have great solution!
The above algo is wrong, if you input "avixdefdiva" it gives output as 3, but it should be -1 as by removing 3rd index the resultant string ("avidefdiva") won't be a palindrome.
For string - "abcdefghzxqwgnbam", your code is returning 16, but it can never become a pelindrom and hence should return -1.
My simple solution is like this :
Rather than checking for only the next character after a mismatch,check for the next two characters.
Yea these cases are bit tricky ones you need to backtrack to the postion you have earlier selected n choose different path(i.e if you choose 8 for deletion now select len-8) when characters are not matching.
The string: "hgygsvlfcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcwflvsgygh" is present in testcase #11, and also in testcase #1. In #1, if the code returns 8, it's fine, but en #11, return of an 8 fails the test
complete solution https://www.hackerrank.com/challenges/palindrome-index/forum/comments/474736
I made another approach for this problem in Python 3: * invert all the string and store in s_i * Iterarate s removing postion from start to end and iterate s_i removing postion from end to start * if s and s_i are equals, return postion
This solution pass in all test cases.
no response on stdout
I think this would help
You need another test before you return i. For example, this fails if s="abcdefxdcbaa"
My code in C++:
You are right . same problem
I was getting th same issue and I adjusted my code to suit the outputs. Here's my JS solution:
6 years have elapsed since you posted this error and it still remains there. Testcase 11 and 14 fails.
We also need to check whether the subsequent elements are palindrome, as only one removal is needed we just check for the next elements to be equal.
int palindromeIndex(string s) {
}
same happening wih me.
Edit: New Solution
How about this?
okay....thnks @vishalbty, i don't y it initially looked me so complex :)
This question is little bit weird . You have to assume that a given string is a pallindrome or just off by a single character . So basically you have to run a loop from both starting and ending and check which characters dont match. Now you have to remove any one of the character and check if the string is pallindrome .If yes return the index of the character removed or else return the index of other character (the character which didnt match at the start) .Just look at this code and you will understand what i was taking about .(Its a java code btw )
The problem of test case 11 and 14 is that you have to check two index ahead in both cases hgygsvlfcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcwflvsgygh mismatch at index 8 so if you remove c at index 8 just checking after c is w then it will fail cause it won't be palindrome.
Check you answer for sample case: cwccw You'll get an better understanding. You can check my code, it works....
` python3 code
NO BRO...your string is "hgygsvlfcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcwflvsgygh"
If you remove all middle equal characters it will be
"hgygsvlfcw - nswtuhmyaljkqlqjjqlqkjlaymhutwsn - wcwflvsgygh"
here you find one 'w' is extra...
"hgygsvlfcwwcwflvsgygh"
brother, if we remove 8th letter 'c' we get .....fwns...... front left and from right we get ......fwcwns.... . but if we remove 44 letter we get a palandrome. i faced the same problem , so i sort to check the third letter after mismatch
if s[i]!=s[j]: if s[i]==s[j-1] and s[i+1]==s[j-2]: return j else return i
I had the same problem but I solved the problem with the string you shared. Thank you. My solution is:
for (int i = 0, j = s.Length - 1; i < s.Length / 2 && j >= s.Length / 2; i++, j--) { if (s[i] != s[j]) { if(s[i] == s[j - 1] && s[i + 1] == s[j]) { if (s[i + 1] == s[j - 2]) index = j; else index = i; } else if (s[i + 1] == s[j]) index = i; else if (s[i] == s[j - 1]) index = j; break; } } return index;
Testcase 11 and 14 are CORRECT.
correct explanation to soln is explained by this thread. https://www.hackerrank.com/challenges/palindrome-index/forum/comments/121372
https://www.hackerrank.com/challenges/palindrome-index/forum/comments/60201
no, removing any of both char won't work, it has to be only 'w'(idx 44) because.. the string is "...fcwn...nwcwf..." (dots are all palindrome only you can check), now after char 'f', 'c'(idx 8) and 'w' char are different, now if you remove 'c' here.. the string would be "...fwn...nwcwf..." which doesn't make the string palindrome, but if you remove w, the string will be "...fcwn...nwcf..." which is palindrome, that's why here answer should be 44, not 8
My first algorithm was also failing tests 11 and 14. But I realized it was a problem of the algorithm, not of the test. If you remove 8th caracter from the string "hgygsvlfcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcwflvsgygh" it will not become a palindrome. I have 'isolated' my algorithm problem here.
"cwnnwcw"
We have to remove 6th carachter to form a palindrome.
For anoyone still looking for an O(n) solution (2 pointers) that passes test cases 11 and 14, here's a working C# solution with comments:
the issue with 11 & 14 is that we could remove/skip either the left or right character when iterating through, but only 1 of them leads to a solution, so we have to check the next character as well to see if left or right is the right to remove/skip
The answer is 44. I created an extra check palindrome function, and removing index 8 only does not result in a palindrome. only by removing index 44, will the resulting string be a palindrome.