Maximize product of lengths of strings having no common characters
Last Updated :
17 Jan, 2023
Given an array arr[] consisting of N strings, the task is to find the maximum product of the length of the strings arr[i] and arr[j] for all unique pairs (i, j), where the strings arr[i] and arr[j] contain no common characters.
Examples:
Input: arr[] = {“abcw”, “baz”, “foo”, “bar”, “xtfn”, “abcdef”}
Output: 16
Explanation: The strings “abcw” and “xtfn” have no common characters in it. Therefore, the product of the length of both the strings = 4 * 4 = 16, which is maximum among all possible pairs.
Input: arr[] = {“a”, “aa”, “aaa”, “aaaa”}
Output: 0
Naive Approach: The idea is to generate all the pairs of strings and use a map to find the common character between them, if there are no common character between them, then use the product of their length to maximize the result.
Follow the steps below to implement the above idea:
- Generate all pairs of strings say, s1 and s2.
- Use a map to find the common character between the strings s1 and s2.
- Check if there is any common character between them:
- If there is no common character between them then use the product of their length and maximize the result.
- Finally, return the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximizeProduct(vector<string>& arr)
{
int n = arr.size();
int result = 0;
for ( int i = 0; i < n; i++) {
string s1 = arr[i];
int len1 = arr[i].size();
unordered_map< char , int > unmap;
for ( auto c1 : s1)
unmap[c1]++;
for ( int j = i + 1; j < n; j++) {
string s2 = arr[j];
int len2 = arr[j].size();
bool flag = false ;
for ( int k = 0; k < s2.size(); k++) {
if (unmap.count(s2[k])) {
flag = true ;
break ;
}
}
if (flag == false ) {
result = max(result, len1 * len2);
}
}
}
return result;
}
int main()
{
vector<string> arr
= { "abcw" , "baz" , "foo" , "bar" , "xtfn" , "abcdef" };
int result = maximizeProduct(arr);
cout << result;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int maximizeProduct(String[] arr)
{
int n = arr.length;
int result = 0 ;
for ( int i = 0 ; i < n; i++) {
String s1 = arr[i];
int len1 = arr[i].length();
HashMap<Character, Integer> unmap
= new HashMap<>();
for ( char c : s1.toCharArray()) {
if (unmap.containsKey(c)) {
unmap.put(c, unmap.get(c) + 1 );
}
else
unmap.put(c, 1 );
}
for ( int j = i + 1 ; j < n; j++) {
String s2 = arr[j];
int len2 = arr[j].length();
boolean flag = false ;
for ( int k = 0 ; k < s2.length(); k++) {
if (unmap.containsKey(s2.charAt(k))) {
flag = true ;
break ;
}
}
if (flag == false ) {
result = Math.max(result, len1 * len2);
}
}
}
return result;
}
public static void main(String args[])
{
String[] arr = { "abcw" , "baz" , "foo" ,
"bar" , "xtfn" , "abcdef" };
int result = maximizeProduct(arr);
System.out.println(result);
}
}
|
Python3
def maximizeProduct(arr):
n = len (arr)
result = 0
for i in range (n):
s1 = arr[i]
len1 = len (arr[i])
unmap = {}
for c in s1:
if (c in unmap):
unmap + = 1
unmap = 1
for j in range (i + 1 , n):
s2 = arr[j]
len2 = len (arr[j])
flag = False
for k in range ( len (s2)):
if (s2[k] in unmap):
flag = True
break
if (flag = = False ):
result = max (result, len1 * len2)
return result
arr = [ "abcw" , "baz" , "foo" , "bar" , "xtfn" , "abcdef" ]
result = maximizeProduct(arr)
print (result)
|
C#
using System;
using System.Collections.Generic;
public class Test {
static int maximizeProduct(List< string > arr)
{
int n = arr.Count;
int result = 0;
for ( int i = 0; i < n; i++) {
string s1 = arr[i];
int len1 = arr[i].Length;
Dictionary< char , int > unmap
= new Dictionary< char , int >();
foreach ( char c in s1)
{
if (unmap.ContainsKey(c))
unmap++;
else
unmap.Add(c, 1);
}
for ( int j = i + 1; j < n; j++) {
string s2 = arr[j];
int len2 = arr[j].Length;
bool flag = false ;
for ( int k = 0; k < s2.Length; k++) {
if (unmap.ContainsKey(s2[k])) {
flag = true ;
break ;
}
}
if (flag == false ) {
result = Math.Max(result, len1 * len2);
}
}
}
return result;
}
public static void Main()
{
List< string > arr
= new List< string >{ "abcw" , "baz" , "foo" ,
"bar" , "xtfn" , "abcdef" };
int result = maximizeProduct(arr);
Console.WriteLine(result);
}
}
|
Javascript
<script>
function maximizeProduct(arr)
{
let n = arr.length;
let result = 0;
for (let i = 0; i < n; i++) {
let s1 = arr[i];
let len1 = arr[i].length;
let unmap = new Map();
for (let c of s1){
if (unmap.has(c)){
unmap.set(c,unmap.get(c)+1);
}
unmap.set(c,1);
}
for (let j = i + 1; j < n; j++) {
let s2 = arr[j];
let len2 = arr[j].length;
let flag = false ;
for (let k = 0; k < s2.length; k++) {
if (unmap.has(s2[k])) {
flag = true ;
break ;
}
}
if (flag == false ) {
result = Math.max(result, len1 * len2);
}
}
}
return result;
}
let arr = [ "abcw" , "baz" , "foo" , "bar" , "xtfn" , "abcdef" ];
let result = maximizeProduct(arr);
document.write(result, "</br>" );
</script>
|
Time Complexity: O(N2 * M), where M is the maximum length of the string.
Auxiliary Space: O(M)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...