File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments