Maximum sum of lengths of a pair of strings with no common characters from a given array
Last Updated :
18 Feb, 2022
Given an array arr[] consisting of N strings, the task is to find the maximum sum of length of the strings arr[i] and arr[j] for all unique pairs (i, j), where the strings arr[i] and arr[j] contains no common characters.
Examples:
Input: arr[] = [“abcd”, “cat”, “lto”, “car”, “wxyz”, “abcdef”]
Output: 8
Explanation:
The strings “abcd” and “wxyz” have no common characters in it. Therefore, the sum of the length of both the strings = 4 + 4 = 8, which is maximum among all possible pairs.
Input: arr[] = [“abcd”, “def”, “fghi”, “ijklm”]
Output: 8
Naive Approach: The simplest approach to solve the given problem is to generate all possible pairs of the array of strings and print the maximum value of the sum of the length of strings of pairs having no common characters between them.
Time Complexity: O(N2 * M), where M is the maximum length of the string.
Auxiliary Space: O(M)
Efficient Approach: The above approach can also be optimized by using the idea of Bit Manipulation. the idea is to convert each string into its bitmask integer equivalent and then find the pair of strings having no common characters having the maximum sum of their lengths. Follow the steps below to solve the problem:
- Initialize a vector mask of size N to store the bitwise OR of a string in the array of strings words[].
- Initialize the variable maxLength as 0 to store the answer.
- Iterate over the range [0, N] using the variable i and performing the following tasks:
- Iterate over the range [0, M] where M is the length of the string using the variable j and set the value of mask[i] as mask[i]|1<<(words[i][j] – ‘a’).
- Iterate over the range [0, i] using the variable j and if the value bitwise AND of mask[i] and mask[j] is not 0, then set the value of maxLength as the maximum of maxLength or words[i].length() + words[j].length().
- After completing the above steps, print the value of the maxLength as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSum(vector<string>& words)
{
vector< int > mask(words.size());
int result = 0;
for ( int i = 0; i < words.size(); ++i) {
for ( char c : words[i]) {
mask[i] |= 1 << (c - 'a' );
}
for ( int j = 0; j < i; ++j) {
if (!(mask[i] & mask[j])) {
result
= max(result, int (words[i].size()
+ words[j].size()));
}
}
}
return result;
}
int main()
{
vector<string> words = { "abcd" , "def" ,
"fghi" , "ijklm" };
cout << maxSum(words);
return 0;
}
|
Java
class GFG {
public static int maxSum(String[] words) {
int [] mask = new int [words.length];
int result = 0 ;
for ( int i = 0 ; i < words.length; ++i) {
for ( char c : words[i].toCharArray()) {
mask[i] |= 1 << (c - 'a' );
}
for ( int j = 0 ; j < i; ++j) {
if ((mask[i] & mask[j]) < 1 ) {
result = Math.max(result, ( int ) words[i].length() + words[j].length());
}
}
}
return result;
}
public static void main(String args[]) {
String[] words = { "abcd" , "def" , "fghi" , "ijklm" };
System.out.println(maxSum(words));
}
}
|
Python3
def maxSum(words):
mask = [ 0 ] * len (words)
result = 0
for i in range ( len (words)):
for c in words[i]:
mask[i] | = 1 << ( ord (c) - 97 )
for j in range (i):
if not (mask[i] & mask[j]):
result = max (result, len (words[i]) + len (words[j]))
return result
words = [ "abcd" , "def" , "fghi" , "ijklm" ]
print (maxSum(words))
|
C#
using System;
public class GFG {
public static int maxSum(String[] words)
{
int [] mask = new int [words.Length];
int result = 0;
for ( int i = 0; i < words.Length; ++i) {
foreach ( char c in words[i]) {
mask[i] |= 1 << (c - 'a' );
}
for ( int j = 0; j < i; ++j) {
if ((mask[i] & mask[j]) < 1) {
result = Math.Max(
result, ( int )words[i].Length
+ words[j].Length);
}
}
}
return result;
}
static public void Main()
{
String[] words = { "abcd" , "def" , "fghi" , "ijklm" };
Console.WriteLine(maxSum(words));
}
}
|
Javascript
<script>
function maxSum(words)
{
let mask = new Array(words.length);
let result = 0;
for (let i = 0; i < words.length; ++i)
{
for (let c = words[i]; c < words[i].length; c++)
{
mask[i] |= 1 << (words[i].charCodeAt(0) -
'a' .charCodeAt(0));
}
for (let j = 0; j < i; ++j)
{
if (!(mask[i] & mask[j]))
{
result = Math.max(result, words[i].length +
words[j].length);
}
}
}
return result;
}
let words = [ "abcd" , "def" ,
"fghi" , "ijklm" ];
document.write(maxSum(words));
</script>
|
Time Complexity: O(max(N*M, N2)), where M is the maximum length of the string.
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...