Make a Palindrome by Inserting Characters - Google Top Interview Questions
Problem Statement :
Given a string s, return the minimum number of characters needed to be inserted so that the string becomes a palindrome. Constraints n ≤ 1,000 where n is the length of s Example 1 Input s = "radr" Output 1 Explanation We can insert "a" to get "radar"
Solution :
Solution in C++ :
int solve(string s) {
int n = s.length();
int dp[n + 1][n + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0) {
dp[i][j] = j;
} else if (j == 0) {
dp[i][j] = i;
} else if (s[i - 1] == s[n - j]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = 1 + min(dp[i][j - 1], dp[i - 1][j]);
}
}
}
return dp[n][n] / 2;
}
Solution in Java :
import java.util.*;
class Solution {
public int solve(String s) {
return s.length() - lps(s);
}
int lps(String st) {
char[] s = st.toCharArray();
int m = s.length;
int[][] dp = new int[m][m];
for (int i = 0; i < m; i++) dp[i][i] = 1;
for (int i = m - 1; i >= 0; i--) {
for (int j = i + 1; j < m; j++) {
if (s[i] == s[j]) {
dp[i][j] = dp[i + 1][j - 1] + 2;
} else {
dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
}
}
}
return dp[0][m - 1];
}
}
Solution in Python :
class Solution:
def solve(self, s):
"""
#1: Most popular approach: time and space complexity: O(n^2).
Let f[i][j] be the length of the longest palindromic subsequence in s[i:j+1]
f[i][j] = f[i+1][j-1] + 2 if s[i] == s[j]
= max(f[i+1][j], f[i][j-1]) otherwise
"""
n = len(s)
f = [[0] * n for _ in range(n)]
for size in range(1, n + 1):
for i in range(n - size + 1):
j = i + size - 1
if s[i] == s[j]:
if i + 1 > j - 1:
f[i][j] = size
else:
f[i][j] = f[i + 1][j - 1] + 2
else:
f[i][j] = max(f[i + 1][j], f[i][j - 1])
return n - f[0][n - 1]
"""
#2: A different transition function: time and space complexity: still O(n^2).
But this is a pathway to a better solution.
Let f[size][i] be the length of the longest palindromic subsequence in s[i:i+size]
f[size][i] = f[size-2][i+1] + 2 if s[i] == s[i+size-1]
= max(f[size-1][i+1], f[size-1][i]) otherwise
"""
n = len(s)
f = [[0] * n for _ in range(n + 1)]
for size in range(1, n + 1):
for i in range(n - size + 1):
j = i + size - 1
if s[i] == s[j]:
if size - 2 > 0:
f[size][i] = f[size - 2][i + 1] + 2
else:
f[size][i] = size
else:
f[size][i] = max(f[size - 1][i + 1], f[size - 1][i])
return n - f[n][0]
"""
#3: Based on #2, do rolling array to reduce space: time complexity: still O(n^2), space: O(n).
In #2, notice that f[size] only depends on f[size-1] and f[size-2], so instead of storing the whole
(n+1) arrays, we just need 3 arrays, f0, f1, f2. f0 is the current f[size], f1 is the f[size-1], and
f2 is the f[size-2].
"""
n = len(s)
f0 = [0] * n
f1 = [0] * n
f2 = [0] * n
for size in range(1, n + 1):
f0, f1, f2 = [0] * n, f0, f1
for i in range(n - size + 1):
j = i + size - 1
if s[i] == s[j]:
if size - 2 > 0:
f0[i] = f2[i + 1] + 2
else:
f0[i] = size
else:
f0[i] = max(f1[i + 1], f1[i])
return n - f0[0]
View More Similar Problems
Tree: Inorder Traversal
In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func
View Solution →Tree: Height of a Binary Tree
The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary
View Solution →Tree : Top View
Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.
View Solution →Tree: Level Order Traversal
Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F
View Solution →Binary Search Tree : Insertion
You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <
View Solution →Tree: Huffman Decoding
Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t
View Solution →