-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMakePalindrome.java
More file actions
115 lines (73 loc) · 1.83 KB
/
MakePalindrome.java
File metadata and controls
115 lines (73 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
Given a string A 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 return 1 else return 0.
Problem Constraints
3 <= |A| <= 105
A[i] is always a lowercase character.
Input Format
First and only argument is an string A.
Output Format
Return 1 if it is possible to convert A to palindrome by removing exactly one character else return 0.
Example Input
Input 1:
A = "abcba"
Input 2:
A = "abecbea"
Example Output
Output 1:
1
Output 2:
0
Example Explanation
Explanation 1:
We can remove character ‘c’ to make string palindrome
Explanation 2:
It is not possible to make this string palindrome just by removing one character
**/
public class Solution
{
public int solve(String A)
{
int low = 0, high = A.length()-1;
while(low < high)
{
if(A.charAt(low) != A.charAt(high))
{
if(isPalindrome(A, low+1, high))
{
return 1;
}
if(isPalindrome(A, low, high-1))
{
return 1;
}
return 0;
}
else
{
low++;
high--;
}
}
return 1;
}
/**
This method will check if the given substring provided is Palindrome or not..
**/
public boolean isPalindrome(String s, int low, int high)
{
while(low < high)
{
if(s.charAt(low) != s.charAt(high))
{
return false;
}
else
{
low++;
high--;
}
}
return true;
}
}