Shortest Superstring Problem
Last Updated :
16 Mar, 2024
Given a set of n strings arr[], find the smallest string that contains each string in the given set as substring. We may assume that no string in arr[] is substring of another string.
Examples:
Input: arr[] = {“geeks”, “quiz”, “for”}
Output: geeksquizfor
Explanation: “geeksquizfor” contains all the three strings of arr[]:
- “geeksquizfor” contains “geeks”.
- “geeksquizfor” contains “quiz”.
- “geeksquizfor” contains “for”.
Input: arr[] = {“catg”, “ctaagt”, “gcta”, “ttca”, “atgcatc”}
Output: gctaagttcatgcatc
Explanation:
- “gctaagttcatgcatc” contains “catg”.
- “gctaagttcatgcatc” contains “ctaagt“.
- “gctaagttcatgcatc” contains “gcta“.
- “gctaagttcatgcatc” contains “ttca”.
- “gctaagttcatgcatc” contains “atgcatc”.
Shortest Superstring Greedy Approximate Algorithm
Shortest Superstring Problem is a NP Hard problem. A solution that always finds shortest superstring takes exponential time. Below is an Approximate Greedy algorithm.
Let arr[] be given set of strings.
1) Create an auxiliary array of strings, temp[]. Copy contents
of arr[] to temp[]
2) While temp[] contains more than one strings
a) Find the most overlapping string pair in temp[]. Let this
pair be 'a' and 'b'.
b) Replace 'a' and 'b' with the string obtained after combining
them.
3) The only string left in temp[] is the result, return it.
Two strings are overlapping if prefix of one string is same suffix of other string or vice versa. The maximum overlap mean length of the matching prefix and suffix is maximum.
Working of above Algorithm:
arr[] = {"catgc", "ctaagt", "gcta", "ttca", "atgcatc"}
Initialize:
temp[] = {"catgc", "ctaagt", "gcta", "ttca", "atgcatc"}
The most overlapping strings are "catgc" and "atgcatc"
(Suffix of length 4 of "catgc" is same as prefix of "atgcatc")
Replace two strings with "catgcatc", we get
temp[] = {"catgcatc", "ctaagt", "gcta", "ttca"}
The most overlapping strings are "ctaagt" and "gcta"
(Prefix of length 3 of "ctaagt" is same as suffix of "gcta")
Replace two strings with "gctaagt", we get
temp[] = {"catgcatc", "gctaagt", "ttca"}
The most overlapping strings are "catgcatc" and "ttca"
(Prefix of length 2 of "catgcatc" as suffix of "ttca")
Replace two strings with "ttcatgcatc", we get
temp[] = {"ttcatgcatc", "gctaagt"}
Now there are only two strings in temp[], after combing
the two in optimal way, we get tem[] = {"gctaagttcatgcatc"}
Since temp[] has only one string now, return it.
Below is the implementation of the above algorithm.
C++
// C++ program to find shortest
// superstring using Greedy
// Approximate Algorithm
#include <bits/stdc++.h>
using namespace std;
// Utility function to calculate
// minimum of two numbers
int min(int a, int b)
{
return (a < b) ? a : b;
}
// Function to calculate maximum
// overlap in two given strings
int findOverlappingPair(string str1,
string str2, string &str)
{
// Max will store maximum
// overlap i.e maximum
// length of the matching
// prefix and suffix
int max = INT_MIN;
int len1 = str1.length();
int len2 = str2.length();
// Check suffix of str1 matches
// with prefix of str2
for (int i = 1; i <=
min(len1, len2); i++)
{
// Compare last i characters
// in str1 with first i
// characters in str2
if (str1.compare(len1-i, i, str2,
0, i) == 0)
{
if (max < i)
{
// Update max and str
max = i;
str = str1 + str2.substr(i);
}
}
}
// Check prefix of str1 matches
// with suffix of str2
for (int i = 1; i <=
min(len1, len2); i++)
{
// compare first i characters
// in str1 with last i
// characters in str2
if (str1.compare(0, i, str2,
len2-i, i) == 0)
{
if (max < i)
{
// Update max and str
max = i;
str = str2 + str1.substr(i);
}
}
}
return max;
}
// Function to calculate
// smallest string that contains
// each string in the given
// set as substring.
string findShortestSuperstring(string arr[],
int len)
{
// Run len-1 times to
// consider every pair
while(len != 1)
{
// To store maximum overlap
int max = INT_MIN;
// To store array index of strings
int l, r;
// Involved in maximum overlap
string resStr;
// Maximum overlap
for (int i = 0; i < len; i++)
{
for (int j = i + 1; j < len; j++)
{
string str;
// res will store maximum
// length of the matching
// prefix and suffix str is
// passed by reference and
// will store the resultant
// string after maximum
// overlap of arr[i] and arr[j],
// if any.
int res = findOverlappingPair(arr[i],
arr[j], str);
// check for maximum overlap
if (max < res)
{
max = res;
resStr.assign(str);
l = i, r = j;
}
}
}
// Ignore last element in next cycle
len--;
// If no overlap, append arr[len] to arr[0]
if (max == INT_MIN)
arr[0] += arr[len];
else
{
// Copy resultant string to index l
arr[l] = resStr;
// Copy string at last index to index r
arr[r] = arr[len];
}
}
return arr[0];
}
// Driver program
int main()
{
string arr[] = {"catgc", "ctaagt",
"gcta", "ttca", "atgcatc"};
int len = sizeof(arr)/sizeof(arr[0]);
// Function Call
cout << "The Shortest Superstring is "
<< findShortestSuperstring(arr, len);
return 0;
}
// This code is contributed by Aditya Goel
Java
// Java program to find shortest
// superstring using Greedy
// Approximate Algorithm
import java.io.*;
import java.util.*;
class GFG
{
static String str;
// Utility function to calculate
// minimum of two numbers
static int min(int a, int b)
{
return (a < b) ? a : b;
}
// Function to calculate maximum
// overlap in two given strings
static int findOverlappingPair(String str1,
String str2)
{
// max will store maximum
// overlap i.e maximum
// length of the matching
// prefix and suffix
int max = Integer.MIN_VALUE;
int len1 = str1.length();
int len2 = str2.length();
// check suffix of str1 matches
// with prefix of str2
for (int i = 1; i <=
min(len1, len2); i++)
{
// compare last i characters
// in str1 with first i
// characters in str2
if (str1.substring(len1 - i).compareTo(
str2.substring(0, i)) == 0)
{
if (max < i)
{
// Update max and str
max = i;
str = str1 + str2.substring(i);
}
}
}
// check prefix of str1 matches
// with suffix of str2
for (int i = 1; i <=
min(len1, len2); i++)
{
// compare first i characters
// in str1 with last i
// characters in str2
if (str1.substring(0, i).compareTo(
str2.substring(len2 - i)) == 0)
{
if (max < i)
{
// update max and str
max = i;
str = str2 + str1.substring(i);
}
}
}
return max;
}
// Function to calculate smallest
// string that contains
// each string in the given set as substring.
static String findShortestSuperstring(
String arr[], int len)
{
// run len-1 times to consider every pair
while (len != 1)
{
// To store maximum overlap
int max = Integer.MIN_VALUE;
// To store array index of strings
// involved in maximum overlap
int l = 0, r = 0;
// to store resultant string after
// maximum overlap
String resStr = "";
for (int i = 0; i < len; i++)
{
for (int j = i + 1; j < len; j++)
{
// res will store maximum
// length of the matching
// prefix and suffix str is
// passed by reference and
// will store the resultant
// string after maximum
// overlap of arr[i] and arr[j],
// if any.
int res = findOverlappingPair
(arr[i], arr[j]);
// Check for maximum overlap
if (max < res)
{
max = res;
resStr = str;
l = i;
r = j;
}
}
}
// Ignore last element in next cycle
len--;
// If no overlap,
// append arr[len] to arr[0]
if (max == Integer.MIN_VALUE)
arr[0] += arr[len];
else
{
// Copy resultant string
// to index l
arr[l] = resStr;
// Copy string at last index
// to index r
arr[r] = arr[len];
}
}
return arr[0];
}
// Driver Code
public static void main(String[] args)
{
String[] arr = { "catgc", "ctaagt",
"gcta", "ttca", "atgcatc" };
int len = arr.length;
System.out.println("The Shortest Superstring is " +
findShortestSuperstring(arr, len));
}
}
// This code is contributed by
// sanjeev2552
C#
// C# program to find shortest
// superstring using Greedy
// Approximate Algorithm
using System;
class GFG
{
static String str;
// Utility function to calculate
// minimum of two numbers
static int min(int a, int b)
{
return (a < b) ? a : b;
}
// Function to calculate maximum
// overlap in two given strings
static int findOverlappingPair(String str1,
String str2)
{
// max will store maximum
// overlap i.e maximum
// length of the matching
// prefix and suffix
int max = Int32.MinValue;
int len1 = str1.Length;
int len2 = str2.Length;
// check suffix of str1 matches
// with prefix of str2
for (int i = 1; i <=
min(len1, len2); i++)
{
// compare last i characters
// in str1 with first i
// characters in str2
if (str1.Substring(len1 - i).CompareTo(
str2.Substring(0, i)) == 0)
{
if (max < i)
{
// Update max and str
max = i;
str = str1 + str2.Substring(i);
}
}
}
// check prefix of str1 matches
// with suffix of str2
for (int i = 1; i <=
min(len1, len2); i++)
{
// compare first i characters
// in str1 with last i
// characters in str2
if (str1.Substring(0, i).CompareTo(
str2.Substring(len2 - i)) == 0)
{
if (max < i)
{
// update max and str
max = i;
str = str2 + str1.Substring(i);
}
}
}
return max;
}
// Function to calculate smallest
// string that contains
// each string in the given set as substring.
static String findShortestSuperstring(String []arr, int len)
{
// run len-1 times to consider every pair
while (len != 1)
{
// To store maximum overlap
int max = Int32.MinValue;
// To store array index of strings
// involved in maximum overlap
int l = 0, r = 0;
// to store resultant string after
// maximum overlap
String resStr = "";
for (int i = 0; i < len; i++)
{
for (int j = i + 1; j < len; j++)
{
// res will store maximum
// length of the matching
// prefix and suffix str is
// passed by reference and
// will store the resultant
// string after maximum
// overlap of arr[i] and arr[j],
// if any.
int res = findOverlappingPair
(arr[i], arr[j]);
// Check for maximum overlap
if (max < res)
{
max = res;
resStr = str;
l = i;
r = j;
}
}
}
// Ignore last element in next cycle
len--;
// If no overlap,
// append arr[len] to arr[0]
if (max == Int32.MinValue)
arr[0] += arr[len];
else
{
// Copy resultant string
// to index l
arr[l] = resStr;
// Copy string at last index
// to index r
arr[r] = arr[len];
}
}
return arr[0];
}
// Driver Code
public static void Main(String[] args)
{
String[] arr = { "catgc", "ctaagt",
"gcta", "ttca", "atgcatc" };
int len = arr.Length;
Console.Write("The Shortest Superstring is " +
findShortestSuperstring(arr, len));
}
}
// This code is contributed by shivanisinghss2110
JavaScript
function min(a, b) {
return (a < b) ? a : b;
}
function findOverlappingPair(str1, str2) {
let max = Number.MIN_SAFE_INTEGER;
let len1 = str1.length;
let len2 = str2.length;
let str = "";
for (let i = 1; i <= min(len1, len2); i++) {
if (str1.substring(len1 - i) === str2.substring(0, i)) {
if (max < i) {
max = i;
str = str1 + str2.substring(i);
}
}
}
for (let i = 1; i <= min(len1, len2); i++) {
if (str1.substring(0, i) === str2.substring(len2 - i)) {
if (max < i) {
max = i;
str = str2 + str1.substring(i);
}
}
}
return { max: max, str: str };
}
function findShortestSuperstring(arr) {
let len = arr.length;
while (len !== 1) {
let max = Number.MIN_SAFE_INTEGER;
let l = 0, r = 0;
let resStr = "";
for (let i = 0; i < len; i++) {
for (let j = i + 1; j < len; j++) {
let { max: res, str } = findOverlappingPair(arr[i], arr[j]);
if (max < res) {
max = res;
resStr = str;
l = i;
r = j;
}
}
}
len--;
if (max === Number.MIN_SAFE_INTEGER) {
arr[0] += arr[len];
} else {
arr[l] = resStr;
arr[r] = arr[len];
}
}
return arr[0];
}
let arr = ["catgc", "ctaagt", "gcta", "ttca", "atgcatc"];
console.log("The Shortest Superstring is " + findShortestSuperstring(arr));
Python3
# python code for the above approach
import sys
# Utility function to calculate
# minimum of two numbers
def minimum(a, b):
return a if a < b else b
# Function to calculate maximum
# overlap in two given strings
def findOverlappingPair(str1, str2):
# Max will store maximum
# overlap i.e maximum
# length of the matching
# prefix and suffix
max_len = -sys.maxsize
len1 = len(str1)
len2 = len(str2)
str_ = ""
# Check suffix of str1 matches
# with prefix of str2
for i in range(1, minimum(len1, len2)+1):
# Compare last i characters
# in str1 with first i
# characters in str2
if str1[len1-i:] == str2[:i]:
if max_len < i:
# Update max and str_
max_len = i
str_ = str1 + str2[i:]
# Check prefix of str1 matches
# with suffix of str2
for i in range(1, minimum(len1, len2)+1):
# compare first i characters
# in str1 with last i
# characters in str2
if str1[:i] == str2[len2-i:]:
if max_len < i:
# Update max and str_
max_len = i
str_ = str2 + str1[i:]
return max_len, str_
# Function to calculate
# smallest string that contains
# each string in the given
# set as substring.
def findShortestSuperstring(arr, n):
# Run n-1 times to
# consider every pair
while n != 1:
# To store maximum overlap
max_len = -sys.maxsize
# To store array index of strings
l, r = 0, 0
# Involved in maximum overlap
res_str = ""
# Maximum overlap
for i in range(n):
for j in range(i+1, n):
str_ = ""
# res will store maximum
# length of the matching
# prefix and suffix str is
# passed by reference and
# will store the resultant
# string after maximum
# overlap of arr[i] and arr[j],
# if any.
res, str_ = findOverlappingPair(arr[i], arr[j])
# check for maximum overlap
if max_len < res:
max_len = res
res_str = str_
l, r = i, j
# Ignore last element in next cycle
n -= 1
# If no overlap, append arr[n-1] to arr[0]
if max_len == -sys.maxsize:
arr[0] += arr[n]
else:
# Copy resultant string to index l
arr[l] = res_str
# Copy string at last index to index r
arr[r] = arr[n]
return arr[0]
# Driver program
if __name__ == "__main__":
arr = ["catgc", "ctaagt", "gcta", "ttca", "atgcatc"]
n = len(arr)
# Function Call
print("The Shortest Superstring is", findShortestSuperstring(arr, n))
# this code is contributed by bhardwajji
OutputThe Shortest Superstring is gctaagttcatgcatc
The time complexity of this algorithm is O(n^3 * m), where n is the number of strings in the input array and m is the maximum length of any string in the array. This is because the main loop runs n-1 times and the findOverlappingPair function takes O(m) time, and it is called n^2 times.
The space complexity is O(n * m), which is the space required to store the input array and the result string.
Performance of above algorithm:
The above Greedy Algorithm is proved to be 4 approximate (i.e., length of the superstring generated by this algorithm is never beyond 4 times the shortest possible superstring). This algorithm is conjectured to 2 approximate (nobody has found case where it generates more than twice the worst). Conjectured worst case example is {abk, bkc, bk+1}. For example {“abb”, “bbc”, “bbb”}, the above algorithm may generate “abbcbbb” (if “abb” and “bbc” are picked as first pair), but the actual shortest superstring is “abbbc”. Here ratio is 7/5, but for large k, ration approaches 2.
Another Approach:
By “greedy approach” I mean: each time we merge the two strings with a maximum length of overlap, remove them from the string array, and put the merged string into the string array.
Then the problem becomes to: find the shortest path in this graph which visits every node exactly once. This is a Travelling Salesman Problem.
Apply Travelling Salesman Problem DP solution. Remember to record the path.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the overlap between two strings
int calcOverlap(string a, string b) {
for (int i = 1; i < a.length(); i++) {
if (b.find(a.substr(i)) == 0) {
return b.length() - a.length() + i;
}
}
return b.length();
}
// Function to calculate the shortest superstring
string shortestSuperstring(vector<string> A) {
int n = A.size();
vector<vector<int>> graph(n, vector<int>(n));
// Build the graph
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
graph[i][j] = calcOverlap(A[i], A[j]);
graph[j][i] = calcOverlap(A[j], A[i]);
}
}
// Create dp and path arrays
vector<vector<int>> dp(1 << n, vector<int>(n));
vector<vector<int>> path(1 << n, vector<int>(n));
int last = -1, minVal = INT_MAX;
// Start TSP DP
for (int i = 1; i < (1 << n); i++) {
fill(dp[i].begin(), dp[i].end(), INT_MAX);
for (int j = 0; j < n; j++) {
if ((i & (1 << j)) > 0) {
int prev = i - (1 << j);
if (prev == 0) {
dp[i][j] = A[j].length();
} else {
for (int k = 0; k < n; k++) {
if (dp[prev][k] < INT_MAX && dp[prev][k] + graph[k][j] < dp[i][j]) {
dp[i][j] = dp[prev][k] + graph[k][j];
path[i][j] = k;
}
}
}
}
if (i == (1 << n) - 1 && dp[i][j] < minVal) {
minVal = dp[i][j];
last = j;
}
}
}
// Build the path
string res;
int cur = (1 << n) - 1;
stack<int> s;
while (cur > 0) {
s.push(last);
int temp = cur;
cur -= (1 << last);
last = path[temp][last];
}
// Build the result
int i = s.top();
s.pop();
res += A[i];
while (!s.empty()) {
int j = s.top();
s.pop();
res += A[j].substr(A[j].length() - graph[i][j]);
i = j;
}
return res;
}
int main() {
vector<string> arr{"catgc", "ctaagt", "gcta", "ttca", "atgcatc"};
cout << "The Shortest Superstring is " << shortestSuperstring(arr) << endl;
return 0;
}
Java
// Java program for above approach
import java.io.*;
import java.util.*;
class Solution
{
// Function to calculate shortest
// super string
public static String shortestSuperstring(
String[] A)
{
int n = A.length;
int[][] graph = new int[n][n];
// Build the graph
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
graph[i][j] = calc(A[i], A[j]);
graph[j][i] = calc(A[j], A[i]);
}
}
// Creating dp array
int[][] dp = new int[1 << n][n];
// Creating path array
int[][] path = new int[1 << n][n];
int last = -1, min = Integer.MAX_VALUE;
// start TSP DP
for (int i = 1; i < (1 << n); i++)
{
Arrays.fill(dp[i], Integer.MAX_VALUE);
// Iterate j from 0 to n - 1
for (int j = 0; j < n; j++)
{
if ((i & (1 << j)) > 0)
{
int prev = i - (1 << j);
// Check if prev is zero
if (prev == 0)
{
dp[i][j] = A[j].length();
}
else
{
// Iterate k from 0 to n - 1
for (int k = 0; k < n; k++)
{
if (dp[prev][k] < Integer.MAX_VALUE &&
dp[prev][k] + graph[k][j] < dp[i][j])
{
dp[i][j] = dp[prev][k] + graph[k][j];
path[i][j] = k;
}
}
}
}
if (i == (1 << n) - 1 && dp[i][j] < min)
{
min = dp[i][j];
last = j;
}
}
}
// Build the path
StringBuilder sb = new StringBuilder();
int cur = (1 << n) - 1;
// Creating a stack
Stack<Integer> stack = new Stack<>();
// Until cur is zero
// push last
while (cur > 0)
{
stack.push(last);
int temp = cur;
cur -= (1 << last);
last = path[temp][last];
}
// Build the result
int i = stack.pop();
sb.append(A[i]);
// Until stack is empty
while (!stack.isEmpty())
{
int j = stack.pop();
sb.append(A[j].substring(A[j].length() -
graph[i][j]));
i = j;
}
return sb.toString();
}
// Function to check
public static int calc(String a, String b)
{
for (int i = 1; i < a.length(); i++)
{
if (b.startsWith(a.substring(i)))
{
return b.length() - a.length() + i;
}
}
// Return size of b
return b.length();
}
// Driver Code
public static void main(String[] args)
{
String[] arr = { "catgc", "ctaagt",
"gcta", "ttca", "atgcatc" };
// Function Call
System.out.println("The Shortest Superstring is " +
shortestSuperstring(arr));
}
}
C#
using System;
using System.Collections.Generic;
class Program
{
// Function to calculate the overlap between two strings
static int CalcOverlap(string a, string b)
{
for (int i = 1; i < a.Length; i++)
{
if (b.IndexOf(a.Substring(i)) == 0)
{
return b.Length - a.Length + i;
}
}
return b.Length;
}
// Function to calculate the shortest superstring
static string ShortestSuperstring(List<string> A)
{
int n = A.Count;
int[,] graph = new int[n, n];
// Build the graph
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
graph[i, j] = CalcOverlap(A[i], A[j]);
graph[j, i] = CalcOverlap(A[j], A[i]);
}
}
// Create dp and path arrays
int[][] dp = new int[1 << n][];
int[][] path = new int[1 << n][];
int last = -1, minVal = int.MaxValue;
for (int i = 0; i < (1 << n); i++)
{
dp[i] = new int[n];
path[i] = new int[n];
for (int j = 0; j < n; j++)
{
dp[i][j] = int.MaxValue;
}
}
// Start TSP DP
for (int mask = 1; mask < (1 << n); mask++)
{
for (int j = 0; j < n; j++)
{
if ((mask & (1 << j)) > 0)
{
int prevMask = mask - (1 << j);
if (prevMask == 0)
{
dp[mask][j] = A[j].Length;
}
else
{
for (int k = 0; k < n; k++)
{
if (dp[prevMask][k] < int.MaxValue &&
dp[prevMask][k] + graph[k, j] < dp[mask][j])
{
dp[mask][j] = dp[prevMask][k] + graph[k, j];
path[mask][j] = k;
}
}
}
}
if (mask == (1 << n) - 1 && dp[mask][j] < minVal)
{
minVal = dp[mask][j];
last = j;
}
}
}
// Build the path
string res = string.Empty;
int currentMask = (1 << n) - 1;
Stack<int> s = new Stack<int>();
while (currentMask > 0)
{
s.Push(last);
int tempMask = currentMask;
currentMask -= (1 << last);
last = path[tempMask][last];
}
// Build the result
int firstStringIndex = s.Pop();
res += A[firstStringIndex];
while (s.Count > 0)
{
int nextStringIndex = s.Pop();
res += A[nextStringIndex].
Substring(A[nextStringIndex].Length - graph[firstStringIndex,
nextStringIndex]);
firstStringIndex = nextStringIndex;
}
return res;
}
static void Main()
{
List<string> arr = new List<string> { "catgc", "ctaagt", "gcta", "ttca", "atgcatc" };
Console.WriteLine("The Shortest Superstring is " + ShortestSuperstring(arr));
}
}
// This code is contributed by akshitaguprzj3
JavaScript
// Function to calculate shortest super string
function shortestSuperstring(A) {
let n = A.length;
let graph = new Array(n).fill(0).map(() => new Array(n).fill(0));
// Build the graph
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
graph[i][j] = calc(A[i], A[j]);
graph[j][i] = calc(A[j], A[i]);
}
}
// Creating dp array
let dp = new Array(1 << n).fill(0).map(() => new Array(n).fill(0));
// Creating path array
let path = new Array(1 << n).fill(0).map(() => new Array(n).fill(0));
let last = -1, min = Number.MAX_VALUE;
// start TSP DP
for (let i = 1; i < (1 << n); i++) {
dp[i].fill(Number.MAX_VALUE);
// Iterate j from 0 to n - 1
for (let j = 0; j < n; j++) {
if ((i & (1 << j)) > 0) {
let prev = i - (1 << j);
// Check if prev is zero
if (prev == 0) {
dp[i][j] = A[j].length;
} else {
// Iterate k from 0 to n - 1
for (let k = 0; k < n; k++) {
if (dp[prev][k] < Number.MAX_VALUE && dp[prev][k] + graph[k][j] < dp[i][j]) {
dp[i][j] = dp[prev][k] + graph[k][j];
path[i][j] = k;
}
}
}
}
if (i == (1 << n) - 1 && dp[i][j] < min) {
min = dp[i][j];
last = j;
}
}
}
// Build the path
let sb = "";
let cur = (1 << n) - 1;
// Creating a stack
let stack = [];
// Until cur is zero
// push last
while (cur > 0) {
stack.push(last);
let temp = cur;
cur -= (1 << last);
last = path[temp][last];
}
// Build the result
let i = stack.pop();
sb += A[i];
// Until stack is empty
while (stack.length > 0) {
let j = stack.pop();
sb += A[j].substring(A[j].length - graph[i][j]);
i = j;
}
return sb;
}
// Function to check
function calc(a, b) {
for (let i = 1; i < a.length; i++) {
if (b.startsWith(a.substring(i))) {
return b.length - a.length + i;
}
}
// Return size of b
return b.length;
}
// Driver Code
let arr = ["catgc", "ctaagt", "gcta", "ttca", "atgcatc"];
// Function Call
console.log("The Shortest Superstring is " + shortestSuperstring(arr));
Python3
# Python program for the above approach
def shortestSuperstring(A):
n = len(A)
graph = [[0 for i in range(n)] for j in range(n)]
# Build the graph
for i in range(n):
for j in range(n):
graph[i][j] = calc(A[i], A[j])
graph[j][i] = calc(A[j], A[i])
# Creating dp array
dp = [[0 for i in range(n)] for j in range(1 << n)]
# Creating path array
path = [[0 for i in range(n)] for j in range(1 << n)]
last = -1
min_val = float('inf')
# start TSP DP
for i in range(1, (1 << n)):
for j in range(n):
dp[i][j] = float('inf')
for j in range(n):
if (i & (1 << j)) > 0:
prev = i - (1 << j)
# Check if prev is zero
if prev == 0:
dp[i][j] = len(A[j])
else:
# Iterate k from 0 to n - 1
for k in range(n):
if dp[prev][k] < float('inf') and dp[prev][k] + graph[k][j] < dp[i][j]:
dp[i][j] = dp[prev][k] + graph[k][j]
path[i][j] = k
if i == (1 << n) - 1 and dp[i][j] < min_val:
min_val = dp[i][j]
last = j
# Build the path
sb = ""
cur = (1 << n) - 1
# Creating a stack
stack = []
# Until cur is zero
# push last
while cur > 0:
stack.append(last)
temp = cur
cur -= (1 << last)
last = path[temp][last]
# Build the result
i = stack.pop()
sb += A[i]
# Until stack is empty
while len(stack) > 0:
j = stack.pop()
sb += A[j][len(A[j]) - graph[i][j]:]
i = j
return sb
# Function to check
def calc(a, b):
for i in range(1, len(a)):
if b.startswith(a[i:]):
return len(b) - len(a) + i
# Return size of b
return len(b)
# Driver Code
if __name__ == '__main__':
arr = [ "catgc", "ctaagt", "gcta", "ttca", "atgcatc" ]
# Function Call
print("The Shortest Superstring is " + shortestSuperstring(arr))
OutputThe Shortest Superstring is gctaagttcatgcatc
Time complexity: O(n^2 * 2^n), where N is the length of the string array.
Auxiliary Space: O(2^N * N).
There exist better approximate algorithms for this problem. Please refer to below link.
Shortest Superstring Problem | Set 2 (Using Set Cover)
Another Approach Using Bitmask and Dynamic Programming:
This is actually bitmasking problem: if we look at our strings as nodes, then we can evaluate distance between one string and another, for example for abcde and cdefghij distance is 5, because we need to use 5 more symbols fghij to continue first string to get the second. Note, that this is not symmetric, so our graph is oriented.
C++
#include <bits/stdc++.h>
using namespace std;
int tsp(int city, int mask, vector<vector<int>> &distance, vector<vector<int>> &dp, vector<vector<int>> &path, int n) {
if (mask == (1 << n) - 1) return 0;
if (dp[mask][city] != -1) {
return dp[mask][city];
}
int ans = INT_MAX;
int nextCity = -1;
for (int i = 0; i < n; i++) {
if ((mask & (1 << i)) == 0) {
int dis = distance[city][i] + tsp(i, (mask | (1 << i)), distance, dp, path, n);
if (dis < ans) {
ans = dis;
nextCity = i;
}
}
}
path[mask][city] = nextCity;
dp[mask][city] = ans;
return ans;
}
string createPath(vector<string> &words, int start, vector<vector<int>> &distance, vector<vector<int>> &path) {
int c = start;
string result = words[start];
int mask = (1 << start);
int x = path[mask][start];
while (x != -1) {
result += words[x].substr(distance[c][x]);
mask |= (1 << x);
c = x;
x = path[mask][x];
}
return result;
}
string shortestSuperstring(vector<string> words) {
int n = words.size();
vector<vector<int>> distance(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int minLen = min(words[i].length(), words[j].length());
for (int k = minLen; k >= 0; k--) {
if (words[i].substr(words[i].length() - k) == words[j].substr(0, k)) {
distance[i][j] = words[j].length() - k;
break;
}
}
}
}
vector<vector<int>> dp(1 << n, vector<int>(n, -1));
vector<vector<int>> path(1 << n, vector<int>(n, -1));
string ans = "";
int len = INT_MAX;
for (int i = 0; i < n; i++) {
int tspResult = tsp(i, 0, distance, dp, path, n);
string str = createPath(words, i, distance, path);
if (str.length() < len) {
ans = str;
len = str.length();
}
}
return ans;
}
int main() {
vector<string> arr = {"catgc", "ctaagt", "gcta", "ttca", "atgcatc"};
cout << "The Shortest Superstring is " << shortestSuperstring(arr) << endl;
return 0;
}
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ShortestSuperstring {
private static int tsp(int city, int mask, int[][] distance, int[][] dp, int[][] path, int n) {
if (mask == (1 << n) - 1) return 0;
if (dp[mask][city] != -1) {
return dp[mask][city];
}
int ans = Integer.MAX_VALUE;
int nextCity = -1;
for (int i = 0; i < n; i++) {
if ((mask & (1 << i)) == 0) {
int dis = distance[city][i] + tsp(i, (mask | (1 << i)), distance, dp, path, n);
if (dis < ans) {
ans = dis;
nextCity = i;
}
}
}
path[mask][city] = nextCity;
dp[mask][city] = ans;
return ans;
}
private static String createPath(List<String> words, int start, int[][] distance, int[][] path) {
int c = start;
StringBuilder result = new StringBuilder(words.get(start));
int mask = (1 << start);
int x = path[mask][start];
while (x != -1) {
result.append(words.get(x).substring(distance[c][x]));
mask |= (1 << x);
c = x;
x = path[mask][x];
}
return result.toString();
}
private static String shortestSuperstring(List<String> words) {
int n = words.size();
int[][] distance = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int minLen = Math.min(words.get(i).length(), words.get(j).length());
for (int k = minLen; k >= 0; k--) {
if (words.get(i).substring(words.get(i).length() - k).equals(words.get(j).substring(0, k))) {
distance[i][j] = words.get(j).length() - k;
break;
}
}
}
}
int[][] dp = new int[1 << n][n];
int[][] path = new int[1 << n][n];
for (int i = 0; i < (1 << n); i++) {
Arrays.fill(dp[i], -1);
Arrays.fill(path[i], -1);
}
String ans = "";
int len = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
int tspResult = tsp(i, 0, distance, dp, path, n);
String str = createPath(words, i, distance, path);
if (str.length() < len) {
ans = str;
len = str.length();
}
}
return ans;
}
public static void main(String[] args) {
List<String> arr = Arrays.asList("catgc", "ctaagt", "gcta", "ttca", "atgcatc");
System.out.println("The Shortest Superstring is " + shortestSuperstring(arr));
}
}
Python
def overlap(s1, s2):
"""
Finds the maximum overlap between two strings.
Args:
s1: First string.
s2: Second string.
Returns:
Maximum overlap between s1 and s2.
"""
max_overlap = min(len(s1), len(s2))
for i in range(max_overlap, 0, -1):
if s1.endswith(s2[:i]):
return i
return 0
def merge_strings(s1, s2, overlap_len):
"""
Merges two strings based on the given overlap length.
Args:
s1: First string.
s2: Second string.
overlap_len: Length of the overlap between s1 and s2.
Returns:
Merged string of s1 and s2.
"""
return s1 + s2[overlap_len:]
def shortest_superstring(words):
"""
Finds the shortest superstring by merging the input list of words.
Args:
words: A list of strings.
Returns:
The shortest superstring formed by merging the words with minimal overlap.
"""
while len(words) > 1:
max_overlap = -1
merge_indices = None
merged_string = None
for i in range(len(words)):
for j in range(len(words)):
if i != j:
curr_overlap = overlap(words[i], words[j])
if curr_overlap > max_overlap:
max_overlap = curr_overlap
merge_indices = (i, j)
merged_string = merge_strings(words[i], words[j], curr_overlap)
if merge_indices is not None:
i, j = merge_indices
del words[j]
words[i] = merged_string
return words[0]
# Example usage
words = ["catgc", "ctaagt", "gcta", "ttca", "atgcatc"]
shortest_string = shortest_superstring(words)
print("The Shortest Superstring is:", shortest_string)
#this code is contributed by Adarsh
C#
using System;
using System.Collections.Generic;
class Program
{
static int tsp(int city, int mask, int[][] distance, int[][] dp, int[][] path, int n)
{
if (mask == (1 << n) - 1) return 0;
if (dp[mask][city] != -1)
{
return dp[mask][city];
}
int ans = Int32.MaxValue;
int nextCity = -1;
for (int i = 0; i < n; i++)
{
if ((mask & (1 << i)) == 0)
{
int dis = distance[city][i] + tsp(i, (mask | (1 << i)), distance, dp, path, n);
if (dis < ans)
{
ans = dis;
nextCity = i;
}
}
}
path[mask][city] = nextCity;
dp[mask][city] = ans;
return ans;
}
static string createPath(List<string> words, int start, int[][] distance, int[][] path)
{
int c = start;
string result = words[start];
int mask = (1 << start);
int x = path[mask][start];
while (x != -1)
{
result += words[x].Substring(distance[c][x]);
mask |= (1 << x);
c = x;
x = path[mask][x];
}
return result;
}
static string shortestSuperstring(List<string> words)
{
int n = words.Count;
int[][] distance = new int[n][];
for (int i = 0; i < n; i++)
{
distance[i] = new int[n];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i != j)
{
int minLen = Math.Min(words[i].Length, words[j].Length);
for (int k = minLen; k >= 0; k--)
{
if (words[i].Substring(words[i].Length - k) == words[j].Substring(0, k))
{
distance[i][j] = words[j].Length - k;
break;
}
}
}
}
}
int[][] dp = new int[1 << n][];
int[][] path = new int[1 << n][];
for (int i = 0; i < (1 << n); i++)
{
dp[i] = new int[n];
path[i] = new int[n];
for (int j = 0; j < n; j++)
{
dp[i][j] = -1;
path[i][j] = -1;
}
}
string ans = "";
int len = Int32.MaxValue;
for (int i = 0; i < n; i++)
{
tsp(i, 1 << i, distance, dp, path, n);
string str = createPath(words, i, distance, path);
if (str.Length < len)
{
ans = str;
len = str.Length;
}
}
return ans;
}
static void Main(string[] args)
{
List<string> arr = new List<string> { "catgc", "ctaagt", "gcta", "ttca", "atgcatc" };
Console.WriteLine("The Shortest Superstring is " + shortestSuperstring(arr));
}
}
//This code is contributed by Utkarsh
JavaScript
function tsp(city, mask, distance, dp, path, n) {
if (mask == (1 << n) - 1) return 0;
if (dp[mask][city] !== -1) {
return dp[mask][city];
}
let ans = Number.MAX_SAFE_INTEGER;
let nextCity = -1;
for (let i = 0; i < n; i++) {
if ((mask & (1 << i)) === 0) {
const dis = distance[city][i] + tsp(i, (mask | (1 << i)), distance, dp, path, n);
if (dis < ans) {
ans = dis;
nextCity = i;
}
}
}
path[mask][city] = nextCity;
dp[mask][city] = ans;
return ans;
}
function createPath(words, start, distance, path) {
let c = start;
let result = words[start];
let mask = (1 << start);
let x = path[mask][start];
while (x !== -1) {
result += words[x].substring(distance[c][x]);
mask |= (1 << x);
c = x;
x = path[mask][x];
}
return result;
}
function shortestSuperstring(words) {
const n = words.length;
const distance = Array.from({ length: n }, () => Array(n).fill(0));
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
const minLen = Math.min(words[i].length, words[j].length);
for (let k = minLen; k >= 0; k--) {
if (words[i].substring(words[i].length - k) === words[j].substring(0, k)) {
distance[i][j] = words[j].length - k;
break;
}
}
}
}
const dp = Array.from({ length: 1 << n }, () => Array(n).fill(-1));
const path = Array.from({ length: 1 << n }, () => Array(n).fill(-1));
let ans = "";
let len = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < n; i++) {
for (let j = 0; j < (1 << n); j++) {
dp[j] = Array(n).fill(-1);
path[j] = Array(n).fill(-1);
}
const tspResult = tsp(i, 0, distance, dp, path, n);
const str = createPath(words, i, distance, path);
if (str.length < len) {
ans = str;
len = str.length;
}
}
return ans;
}
// Example usage
const arr = ["catgc", "ctaagt", "gcta", "ttca", "atgcatc"];
console.log("The Shortest Superstring is", shortestSuperstring(arr));
OutputThe Shortest Superstring is catgccatcagta
Time complexity: O(2^n*n^2*M), where M is the length of answer
Auxiliary Space: O(2^n*n*M) as well.
Similar Reads
String in Data Structure
A string is a sequence of characters. The following facts make string an interesting data structure. Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immu
3 min read
Introduction to Strings - Data Structure and Algorithm Tutorials
Strings are sequences of characters. The differences between a character array and a string are, a string is terminated with a special character â\0â and strings are typically immutable in most of the programming languages like Java, Python and JavaScript. Below are some examples of strings: "geeks"
7 min read
Applications, Advantages and Disadvantages of String
The String data structure is the backbone of programming languages and the building blocks of communication. String data structures are one of the most fundamental and widely used tools in computer science and programming. They allow for the representation and manipulation of text and character sequ
6 min read
String, Subsequence & Substring
What is a Substring? A substring is a contiguous part of a string, i.e., a string inside another string. In general, for an string of size n, there are n*(n+1)/2 non-empty substrings. For example, Consider the string "geeks", There are 15 non-empty substrings. The subarrays are: g, ge, gee, geek, ge
6 min read
Storage for Strings in C
In C, a string can be referred to either using a character pointer or as a character array. Strings as character arrays [GFGTABS] C char str[4] = "GfG"; /*One extra for string terminator*/ /* OR */ char str[4] = {âGâ, âfâ, âGâ, '\0'}; /* '\0' is string terminator */ [/GFGTA
5 min read
Strings in different language
Basic operations on String
Searching For Characters and Substring in a String in Java
Efficient String manipulation is very important in Java programming especially when working with text-based data. In this article, we will explore essential methods like indexOf(), contains(), and startsWith() to search characters and substrings within strings in Java. Searching for a Character in a
5 min read
Reverse a String â Complete Tutorial
Given a string s, the task is to reverse the string. Reversing a string means rearranging the characters such that the first character becomes the last, the second character becomes second last and so on. Examples: Input: s = "GeeksforGeeks"Output: "skeeGrofskeeG"Explanation : The first character G
14 min read
Left Rotation of a String
Given a string s and an integer d, the task is to left rotate the string by d positions. Examples: Input: s = "GeeksforGeeks", d = 2Output: "eksforGeeksGe" Explanation: After the first rotation, string s becomes "eeksforGeeksG" and after the second rotation, it becomes "eksforGeeksGe". Input: s = "q
15+ min read
Sort string of characters
Given a string of lowercase characters from 'a' - 'z'. We need to write a program to print the characters of this string in sorted order. Examples: Input : "dcab" Output : "abcd"Input : "geeksforgeeks"Output : "eeeefggkkorss" Naive Approach - O(n Log n) TimeA simple approach is to use sorting algori
5 min read
Frequency of Characters in Alphabetical Order
Given a string s, the task is to print the frequency of each of the characters of s in alphabetical order.Example: Input: s = "aabccccddd" Output: a2b1c4d3 Since it is already in alphabetical order, the frequency of the characters is returned for each character. Input: s = "geeksforgeeks" Output: e4
9 min read
Swap characters in a String
Given a String S of length N, two integers B and C, the task is to traverse characters starting from the beginning, swapping a character with the character after C places from it, i.e. swap characters at position i and (i + C)%N. Repeat this process B times, advancing one position at a time. Your ta
14 min read
C Program to Find the Length of a String
The length of a string is the number of characters in it without including the null character (â\0â). In this article, we will learn how to find the length of a string in C. The easiest way to find the string length is by using strlen() function from the C strings library. Let's take a look at an ex
2 min read
How to insert characters in a string at a certain position?
Given a string str and an array of indices chars[] that describes the indices in the original string where the characters will be added. For this post, let the character to be inserted in star (*). Each star should be inserted before the character at the given index. Return the modified string after
7 min read
Check if two strings are same or not
Given two strings, the task is to check if these two strings are identical(same) or not. Consider case sensitivity. Examples: Input: s1 = "abc", s2 = "abc" Output: Yes Input: s1 = "", s2 = "" Output: Yes Input: s1 = "GeeksforGeeks", s2 = "Geeks" Output: No Approach - By Using (==) in C++/Python/C#,
7 min read
Concatenating Two Strings in C
Concatenating two strings means appending one string at the end of another string. In this article, we will learn how to concatenate two strings in C. The most straightforward method to concatenate two strings is by using strcat() function. Let's take a look at an example: [GFGTABS] C #include <s
3 min read
Remove all occurrences of a character in a string
Given a string and a character, remove all the occurrences of the character in the string. Examples: Input : s = "geeksforgeeks" c = 'e'Output : s = "gksforgks"Input : s = "geeksforgeeks" c = 'g'Output : s = "eeksforeeks"Input : s = "geeksforgeeks" c = 'k'Output : s = "geesforgees" Using Built-In Me
2 min read
Binary String
Check if all bits can be made same by single flip
Given a binary string, find if it is possible to make all its digits equal (either all 0's or all 1's) by flipping exactly one bit. Input: 101Output: YeExplanation: In 101, the 0 can be flipped to make it all 1 Input: 11Output: NoExplanation: No matter whichever digit you flip, you will not get the
5 min read
Number of flips to make binary string alternate | Set 1
Given a binary string, that is it contains only 0s and 1s. We need to make this string a sequence of alternate characters by flipping some of the bits, our goal is to minimize the number of bits to be flipped. Examples : Input : str = â001â Output : 1 Minimum number of flips required = 1 We can flip
8 min read
Binary representation of next number
Given a binary input that represents binary representation of positive number n, find a binary representation of n+1.The binary input may be and may not fit even in unsigned long long int. Examples: Input: 10011Output: 10100Explanation:Here n = (19)10 = (10011)2next greater integer = (20)10 = (10100
9 min read
Min flips of continuous characters to make all characters same in a string
Given a string consisting only of 1's and 0's. In one flip we can change any continuous sequence of this string. Find this minimum number of flips so the string consist of same characters only.Examples: Input : 00011110001110Output : 2We need to convert 1's sequenceso string consist of all 0's.Input
8 min read
Generate all binary strings without consecutive 1's
Given an integer, K. Generate all binary strings of size k without consecutive 1's. Examples: Input : K = 3 Output : 000 , 001 , 010 , 100 , 101 Input : K = 4 Output :0000 0001 0010 0100 0101 1000 1001 1010Idea behind that is IF string ends with '1' then we put only '0' at the end. IF string ends wi
15 min read
Find i'th Index character in a binary string obtained after n iterations
Given a decimal number m, convert it into a binary string and apply n iterations. In each iteration, 0 becomes "01" and 1 becomes "10". Find the (based on indexing) index character in the string after the nth iteration. Examples: Input : m = 5, n = 2, i = 3Output : 1Input : m = 3, n = 3, i = 6Output
6 min read
Substring and Subsequence
All substrings of a given String
Given a string s, containing lowercase alphabetical characters. The task is to print all non-empty substrings of the given string. Examples : Input : s = "abc"Output : "a", "ab", "abc", "b", "bc", "c" Input : s = "ab"Output : "a", "ab", "b" Input : s = "a"Output : "a" [Expected Approach] - Using Ite
9 min read
Print all subsequences of a string
Given a string, we have to find out all its subsequences of it. A String is said to be a subsequence of another String, if it can be obtained by deleting 0 or more character without changing its order. Examples: Input : abOutput : "", "a", "b", "ab" Input : abcOutput : "", "a", "b", "c", "ab", "ac",
12 min read
Count Distinct Subsequences
Given a string str of length n, your task is to find the count of distinct subsequences of it. Examples: Input: str = "gfg"Output: 7Explanation: The seven distinct subsequences are "", "g", "f", "gf", "fg", "gg" and "gfg" Input: str = "ggg"Output: 4Explanation: The four distinct subsequences are "",
14 min read
Count distinct occurrences as a subsequence
Given two strings pat and txt, where pat is always shorter than txt, count the distinct occurrences of pat as a subsequence in txt. Examples: Input: txt = abba, pat = abaOutput: 2Explanation: pat appears in txt as below three subsequences.[abba], [abba] Input: txt = banana, pat = banOutput: 3Explana
15+ min read
Longest Common Subsequence (LCS)
Given two strings, s1 and s2, the task is to find the length of the Longest Common Subsequence. If there is no common subsequence, return 0. A subsequence is a string generated from the original string by deleting 0 or more characters, without changing the relative order of the remaining characters.
15+ min read
Shortest Superstring Problem
Given a set of n strings arr[], find the smallest string that contains each string in the given set as substring. We may assume that no string in arr[] is substring of another string.Examples: Input: arr[] = {"geeks", "quiz", "for"}Output: geeksquizforExplanation: "geeksquizfor" contains all the thr
15+ min read
Printing Shortest Common Supersequence
Given two strings s1 and s2, find the shortest string which has both s1 and s2 as its sub-sequences. If multiple shortest super-sequence exists, print any one of them.Examples: Input: s1 = "geek", s2 = "eke"Output: geekeExplanation: String "geeke" has both string "geek" and "eke" as subsequences.Inp
9 min read
Shortest Common Supersequence
Given two strings s1 and s2, the task is to find the length of the shortest string that has both s1 and s2 as subsequences. Examples: Input: s1 = "geek", s2 = "eke"Output: 5Explanation: String "geeke" has both string "geek" and "eke" as subsequences. Input: s1 = "AGGTAB", s2 = "GXTXAYB"Output: 9Expl
15+ min read
Longest Repeating Subsequence
Given a string s, the task is to find the length of the longest repeating subsequence, such that the two subsequences don't have the same string character at the same position, i.e. any ith character in the two subsequences shouldn't have the same index in the original string. Examples: Input: s= "a
15+ min read
Longest Palindromic Subsequence (LPS)
Given a string s, find the length of the Longest Palindromic Subsequence in it. Note: The Longest Palindromic Subsequence (LPS) is the maximum-length subsequence of a given string that is also a Palindrome. Examples: Input: s = "bbabcbcab"Output: 7Explanation: Subsequence "babcbab" is the longest su
15+ min read
Longest Palindromic Substring
Given a string s, the task is to find the longest substring which is a palindrome. If there are multiple answers, then return the first appearing substring. Examples: Input: s = "forgeeksskeegfor" Output: "geeksskeeg"Explanation: There are several possible palindromic substrings like "kssk", "ss", "
12 min read
Palindrome
C Program to Check for Palindrome String
A string is said to be palindrome if the reverse of the string is the same as the string. In this article, we will learn how to check whether the given string is palindrome or not using C program. The simplest method to check for palindrome string is to reverse the given string and store it in a tem
4 min read
Check if a given string is a rotation of a palindrome
Given a string, check if it is a rotation of a palindrome. For example your function should return true for "aab" as it is a rotation of "aba". Examples: Input: str = "aaaad" Output: 1 // "aaaad" is a rotation of a palindrome "aadaa" Input: str = "abcd" Output: 0 // "abcd" is not a rotation of any p
15+ min read
Check if characters of a given string can be rearranged to form a palindrome
Given a string, Check if the characters of the given string can be rearranged to form a palindrome. For example characters of "geeksogeeks" can be rearranged to form a palindrome "geeksoskeeg", but characters of "geeksforgeeks" cannot be rearranged to form a palindrome. Recommended PracticeAnagram P
14 min read
Online algorithm for checking palindrome in a stream
Given a stream of characters (characters are received one by one), write a function that prints 'Yes' if a character makes the complete string palindrome, else prints 'No'. Examples: Input: str[] = "abcba"Output: a Yes // "a" is palindrome b No // "ab" is not palindrome c No // "abc" is not palindro
15+ min read
Print all Palindromic Partitions of a String using Bit Manipulation
Given a string, find all possible palindromic partitions of a given string. Note that this problem is different from Palindrome Partitioning Problem, there the task was to find the partitioning with minimum cuts in input string. Here we need to print all possible partitions. Example: Input: nitinOut
10 min read
Minimum Characters to Add at Front for Palindrome
Given a string s, the task is to find the minimum number of characters to be added to the front of s to make it palindrome. A palindrome string is a sequence of characters that reads the same forward and backward. Examples: Input: s = "abc"Output: 2Explanation: We can make above string palindrome as
12 min read
Make largest palindrome by changing at most K-digits
Given a string containing all digits, we need to convert this string to a palindrome by changing at most K digits. If many solutions are possible then print lexicographically largest one.Examples: Input : str = â43435â k = 3Output : "93939" Explanation:Lexicographically largest palindrome after 3 ch
15 min read
Minimum number of deletions to make a string palindrome
Given a string of size 'n'. The task is to remove or delete the minimum number of characters from the string so that the resultant string is a palindrome. Note: The order of characters should be maintained. Examples : Input : aebcbdaOutput : 2Remove characters 'e' and 'd'Resultant string will be 'ab
15+ min read
Minimum insertions to form a palindrome with permutations allowed
Given a string of lowercase letters. Find minimum characters to be inserted in the string so that it can become palindrome. We can change the positions of characters in the string. Examples: Input: geeksforgeeksOutput: 2Explanation: geeksforgeeks can be changed as: geeksroforskeeg or geeksorfroskeeg
5 min read