jakaria_hossain

Hackerrank -Palindrome index

Jun 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define fast()(ios_base::sync_with_stdio(false),cin.tie(NULL));
  4. int ind=-1;
  5. bool ftest(string s)
  6. {
  7. int l=s.size();
  8. int i=0,j=l-1,cnt=0;
  9. while(i<j)
  10. {
  11. if(s[i]!=s[j])
  12. {
  13. cnt++;
  14.  
  15. if(s[i]==s[j-1])
  16. {
  17. ind =j;
  18. j--;
  19. }
  20. else if(s[i+1]==s[j])
  21. {
  22. ind=i;
  23. i++;
  24. }
  25. }
  26. i++,j--;
  27. if(cnt>1)break;
  28. }
  29. if(cnt==0 || cnt==2)return false;
  30. return true;
  31. }
  32. bool stest(string s)
  33. {
  34. int l=s.size();
  35. int i=0,j=l-1,cnt=0;
  36. while(i<j)
  37. {
  38. if(s[i]!=s[j])
  39. {
  40. cnt++;
  41.  
  42. if(s[i+1]==s[j])
  43. {
  44. ind =i;
  45. i++;
  46. }
  47. else if(s[i]==s[j-1])
  48. {
  49. ind=j;
  50. j--;
  51. }
  52. }
  53. i++,j--;
  54. if(cnt>1)break;
  55. }
  56. if(cnt==0 || cnt==2)return false;
  57. return true;
  58. }
  59. int main()
  60. {
  61. fast();
  62. int t;
  63. cin>>t;
  64. while(t--)
  65. {
  66. string s;
  67. cin>>s;
  68. if(ftest(s))cout<<ind<<endl;
  69. else if(stest(s))cout<<ind<<endl;
  70. else cout<<"-1"<<endl;
  71.  
  72. }
  73. }
Add Comment
Please, Sign In to add comment