Skip to content
Related Articles

Related Articles

Maximize product of lengths of strings having no common characters

View Discussion
Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 17 Jun, 2022

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++




// C++ program to find the 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.
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the maximum product of the length of the
// strings
int maximizeProduct(vector<string>& arr)
{
    int n = arr.size();
 
    // This store the maximum product of string's length
    int result = 0;
 
    // Iterate to find the 1st string
    for (int i = 0; i < n; i++) {
        string s1 = arr[i];
        int len1 = arr[i].size();
 
        // Map to store the unique character
        unordered_map<char, int> unmap;
        for (auto c : s1)
            unmap++;
 
        // Iterate to find the 2nd string
        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++) {
 
                // Check if the characters of s2 are common
                // with s1 characters or not
                if (unmap.count(s2[k])) {
                    flag = true;
                    break;
                }
            }
 
            // This verify that there is no common character
            // between s1 and s2.
            if (flag == false) {
                result = max(result, len1 * len2);
            }
        }
    }
 
    return result;
}
 
// Driver code
int main()
{
    vector<string> arr = { "abcw", "baz",   "foo",
                           "bar""xtfn", "abcdef" };
 
    int result = maximizeProduct(arr);
 
    // Print the output
    cout << result;
 
    return 0;
}

Java




/*package whatever //do not write package name here */
 
import java.io.*;
import java.util.*;
 
class GFG {
// Java program to find the 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.
 
// Function to find the maximum product of the length of the
// strings
static int maximizeProduct(String[] arr)
{
    int n = arr.length;
 
    // This store the maximum product of string's length
    int result = 0;
 
    // Iterate to find the 1st string
    for (int i = 0; i < n; i++) {
        String s1 = arr[i];
        int len1 = arr[i].length();
 
        // Map to store the unique character
        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);
        }
        // Iterate to find the 2nd string
        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++) {
 
                // Check if the characters of s2 are common
                // with s1 characters or not
                if (unmap.containsKey(s2.charAt(k))) {
                    flag = true;
                    break;
                }
            }
 
            // This verify that there is no common character
            // between s1 and s2.
            if (flag == false) {
                result = Math.max(result, len1 * len2);
            }
        }
    }
 
    return result;
}
     
// Driver Code
public static void main(String args[])
{
    String[] arr = { "abcw", "baz",   "foo",
                           "bar""xtfn", "abcdef" };
 
    int result = maximizeProduct(arr);
 
    // Print the output
    System.out.println(result);
}
}

Python3




# Python3 program to find the 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.
 
# Function to find the maximum product of the length of the
# strings
def maximizeProduct(arr):
 
    n = len(arr)
 
    # This store the maximum product of string's length
    result = 0
 
    # Iterate to find the 1st string
    for i in range(n):
        s1 = arr[i]
        len1 = len(arr[i])
 
        # Map to store the unique character
        unmap = {}
        for c in s1:
            if(c in unmap):
                unmap += 1
             
            unmap = 1
 
        # Iterate to find the 2nd string
        for j in range(i+1,n):
            s2 = arr[j]
            len2 = len(arr[j])
 
            flag = False
 
            for k in range(len(s2)):
 
                # Check if the characters of s2 are common
                # with s1 characters or not
                if (s2[k] in  unmap):
                    flag = True
                    break
                 
 
            # This verify that there is no common character
            # between s1 and s2.
            if (flag == False):
                result = max(result, len1 * len2)
             
 
    return result
 
# Driver code
arr = [ "abcw", "baz""foo""bar""xtfn", "abcdef" ]
result = maximizeProduct(arr)
 
# Print the output
print(result)
 
# This code is contributed by shinjanpatra

Javascript




<script>
 
// JavaScript program to find the 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.
 
// Function to find the maximum product of the length of the
// strings
function maximizeProduct(arr)
{
    let n = arr.length;
 
    // This store the maximum product of string's length
    let result = 0;
 
    // Iterate to find the 1st string
    for (let i = 0; i < n; i++) {
        let s1 = arr[i];
        let len1 = arr[i].length;
 
        // Map to store the unique character
        let unmap = new Map();
        for (let c of s1){
            if(unmap.has(c)){
                unmap.set(c,unmap.get(c)+1);
            }
            unmap.set(c,1);
        }
 
        // Iterate to find the 2nd string
        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++) {
 
                // Check if the characters of s2 are common
                // with s1 characters or not
                if (unmap.has(s2[k])) {
                    flag = true;
                    break;
                }
            }
 
            // This verify that there is no common character
            // between s1 and s2.
            if (flag == false) {
                result = Math.max(result, len1 * len2);
            }
        }
    }
 
    return result;
}
 
// Driver code
let arr = [ "abcw", "baz""foo""bar""xtfn", "abcdef" ];
let result = maximizeProduct(arr);
 
// Print the output
document.write(result,"</br>");
 
// This code is contributed by shinjanpatra
 
</script>

Output

16

Time Complexity: O(N2 * M), where M is the maximum length of the string.
Auxiliary Space: O(M)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!