-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConvertIntoPalindrome.java
More file actions
93 lines (73 loc) · 1.77 KB
/
ConvertIntoPalindrome.java
File metadata and controls
93 lines (73 loc) · 1.77 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
package com.program.dynamic.programming;
/**
* @author rrohit
*/
public class ConvertIntoPalindrome {
/**
* This method will add characters to make the given word a palindrome.
* Time : O(n+n) = O(n)
* Space: O(n)
* @param word
* @return
*/
public static String convertIntoPalindrome(String word) {
if (word == null || word.isEmpty()) {
return "a";
}
int wordlen = word.length();
if (wordlen == 1){
return word;
}
boolean match[] = new boolean[wordlen];
int left = 0, right = wordlen-1, matchCount = 0;
while (left < right) {
if (word.charAt(left) == word.charAt(right)) {
match[left] = match[right] = true;
matchCount += 2;
left++;
right--;
} else {
match[right] = false;
right--;
}
}
/**
* In case word it self is a palindrome.
*/
if (matchCount == wordlen || matchCount == wordlen-1 && match[wordlen-1]) {
return word;
}
int j = wordlen-1;
StringBuilder sb = new StringBuilder();
while (j > right) {
if (!match[j]) {
sb.append(word.charAt(j));
}
j--;
}
String palindrome = sb.append(word).toString();
return palindrome;
}
/**
* Condition 1 : If First and Last chars should match.
* Condition 2 : Substring (excluding first and last chars) must be palindrome.
* @param word
* @return
*/
public static boolean palindrome(String word) {
if (word == null) {
return false;
}
if (word.isEmpty() || word.length() == 1) {
return true;
}
if (word.charAt(0) != word.charAt(word.length()-1)){
return false;
}
return palindrome(word.substring(1, word.length()-1));
}
public static void main(String[] args) {
//System.out.println(convertIntoPalindrome("poaop"));
System.out.println(convertIntoPalindrome("madam"));
}
}