-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathProblem34.js
107 lines (92 loc) · 3.13 KB
/
Problem34.js
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
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^buildPalindromeByFewestInsertions" }] */
// Problem 34
//
// This problem was asked by Quora.
//
// Given a string, find the palindrome that can be made by inserting the fewest number of characters as possible anywhere in the word.
// If there is more than one palindrome of minimum length that can be made, return the lexicographically earliest one (the first one alphabetically).
//
// For example, given the string "race", you should return "ecarace", since we can add three letters to it (which is the smallest amount to make a palindrome).
// There are seven other palindromes that can be made from "race" by adding three letters, but "ecarace" comes first alphabetically.
//
// As another example, given the string "google", you should return "elgoogle".
//
// O(N^2) Time complexity
// O(N) Space complexity
// N is the length of the string
/**
* Finds the palindrome that can be made by inserting the fewest number of characters as possible anywhere in the word.
* @param {string} word
* @return {string}
*/
function buildPalindromeByFewestInsertions(word) {
// return buildPalindromeByFewestInsertionsR(word);
const memo = new Map();
return buildPalindromeByFewestInsertionsMemo(word, memo);
}
function buildPalindromeByFewestInsertionsR(word) {
if (isPalindrome(word)) return word;
if (word.charAt(0) === word.charAt(word.length - 1)) {
return (
word.charAt(0) +
buildPalindromeByFewestInsertions(word.substring(1, word.length - 1)) +
word.charAt(word.length - 1)
);
}
const pal1 =
word.charAt(0) +
buildPalindromeByFewestInsertions(word.substring(1)) +
word.charAt(0);
const pal2 =
word.charAt(word.length - 1) +
buildPalindromeByFewestInsertions(word.substring(0, word.length - 1)) +
word.charAt(word.length - 1);
if (pal1.length < pal2.length) return pal1;
if (pal1.length > pal2.length) return pal2;
return pal1.localeCompare(pal2) < 0 ? pal1 : pal2;
}
function buildPalindromeByFewestInsertionsMemo(word, memo) {
if (isPalindrome(word)) return word;
if (memo.has(word)) return memo.get(word);
if (word.charAt(0) === word.charAt(word.length - 1)) {
const palindrome =
word.charAt(0) +
buildPalindromeByFewestInsertions(word.substring(1, word.length - 1)) +
word.charAt(word.length - 1);
memo.set(word, palindrome);
return palindrome;
}
const pal1 =
word.charAt(0) +
buildPalindromeByFewestInsertions(word.substring(1)) +
word.charAt(0);
const pal2 =
word.charAt(word.length - 1) +
buildPalindromeByFewestInsertions(word.substring(0, word.length - 1)) +
word.charAt(word.length - 1);
if (pal1.length < pal2.length) {
memo.set(word, pal1);
return pal1;
}
if (pal1.length > pal2.length) {
memo.set(word, pal2);
return pal2;
}
// same length palindromes
if (pal1.localeCompare(pal2) < 0) {
memo.set(word, pal1);
return pal1;
}
memo.set(word, pal2);
return pal2;
}
function isPalindrome(word) {
return (
word ===
word
.split('')
.reverse()
.join('')
);
}
export default buildPalindromeByFewestInsertions;