Given a string S
consisting only of lowercase characters. We need to check whether it is possible to make this string a palindrome after removing exactly one character from this.
If it is possible then print 1
else return 0
.
Input Format
The first line given is an string S
.
Output Format
Print 1
if it is possible to convert S
to palindrome by removing exactly one character else return 0
.
Example 1
Input
abcba
Output
1
Explanation
We can remove character ‘c’ to make string palindrome.
Example 2
Input
abecbea
Output
0
Explanation
It is not possible to make this string palindrome just by removing one character
Constraints
3 <= |S| <= 10^5
S[i]
is always a lowercase character.
Solution of Convert To Palindrome in java:–
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { //your code here Scanner sc=new Scanner(System.in); String s=sc.next(); int index=topalindrome(s); if(index==-1) { System.out.print(0); } if(index==1) { System.out.print(1); } } public static int topalindrome(String s) { int n=s.length(); int a=0,b=n-1; while(a<b) { if(s.charAt(a)==s.charAt(b)) { a++; b--; } else { if(ispalindrome(s.substring(a,b-1))) return 1; if(ispalindrome(s.substring(a+1,b))) return 1; else return -1; } //return 1; } return 1; } public static boolean ispalindrome(String s) { int a=0; int b=s.length()-1; while(a<b) { if(s.charAt(a)==s.charAt(b)) { a++; b--; } else return false; } return true; } }
Add a Comment