Skip to content

Commit 0c88e16

Browse files
authored
Merge pull request #752 from PurviGoyal7/Strings_ConvertToPalindrome
ConvertToPalindrome(String) added in the Strings
2 parents 4ed0b64 + fddebc1 commit 0c88e16

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Given a string A consisting only of lowercase characters, we need to check whether it is possible to make
2+
this string a palindrome after removing exactly one character from this.
3+
4+
If it is possible then print "1" else print "0" (without quotes).
5+
3 <= |A| <= 105
6+
A[i] is always a lowercase character.
7+
8+
Input Format
9+
First and only argument is an string A.
10+
11+
Output Format
12+
Print 1 if it is possible to convert A to palindrome by removing exactly one character else print 0.
13+
*/
14+
15+
16+
# include<bits/stdc++.h>
17+
using namespace std;
18+
19+
int main()
20+
{
21+
string A;
22+
cin>>A;
23+
int n = A.size(); //n is the size of the given string
24+
int i=0, j=n-1, count=0;
25+
while(i<j) // using two pointer approach with O(n) time complexity
26+
{
27+
if(A[i] == A[j]) //comparing first and last character of string
28+
{
29+
i++;
30+
j--;
31+
}
32+
else if(A[i+1]==A[j] && count == 0)
33+
{
34+
count++;
35+
i++;
36+
}
37+
else if(A[i]==A[j-1] && count == 0)
38+
{
39+
count++;
40+
j--;
41+
}
42+
else break;
43+
}
44+
if(i<j)
45+
cout<<"0"<<endl;
46+
else
47+
cout<<"1"<<endl;
48+
}
49+

0 commit comments

Comments
 (0)