You are given a list of ‘N’ words. Your task is to find the rectangle of the maximum area that can be formed using the list of words zero or more times such that the following conditions hold true:
1. Let ‘W’ be a word formed from reading some row from left to right then ‘W’ must be present in the list.
2. Let ‘W’ be a word formed from reading some column from top to down, then ‘W’ must be present in the list.
Note :
If no such rectangle exists then you need to print 0.
Input Format
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the length of the list of words.
The next ‘N’ lines contain a single string denoting the word of the given list.
Output Format:
For each test case, print 0 if no such rectangle exists, else print the maximum area of the rectangle that can be formed.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= N <= 100
1 <= | WORD[ i ] | <= 10
WORD[ i ] contains only lowercase English letters.
Where ‘T’ is the number of test cases, ‘N’ is the length of the list and ‘WORD[ i ]’ is the word of the list at index i.
Time limit: 1 sec.
2
1
aa
2
aaa
b
4
9
Test Case 1 :
Given list = [ “aa”]
Rectangle of area 4 is shown below
aa
aa
Test Case 2 :
Given list = [ “aaa”, “b” ]
Rectangle of area 9 is shown below
aaa
aaa
aaa
2
1
abc
2
coding
ninjas
0
0
Test Case 1 :
Given list = [ “abc” ]
We cannot form a rectangle of any area using the given list.
So we need to print 0.
Test Case 2 :
Given list = [ “coding”, “ninjas” ]
We cannot form a rectangle of any area using the given list.
So we need to print 0.
Try to form rectangles of all possible areas.
The idea here is to check all rectangles of all possible areas. Let ‘maxlen’ be the maximum length of a word present in the given list. Then max area possible will be less than or equal to maxlen * maxlen because each row and column must represent a word.
So we will be try to form all areas from the maximum possible area of the rectangle that is maxlen * maxlen to 1. First, we will insert all words into a hashmap according to their length. Then we will try to form a word of maximum area. To do this we will keep inserting words row by row from the ‘hashMap’.
Then we check if the current rectangle is valid or not. As all rows are already valid since we have inserted each one of them using hashMap. So we need to check the validity of columns that can be done via trie data structure.
Description of our trie node is shown below.
Our trie data structure basically tells us two things
Algorithm:
Description of DFS function:
This function is used to generate all possible rectangles. It will take arguments :
1. ‘word’ – list of current words of the same size.
2. ‘maxArea’ – variable to store ‘maxArea’
3. ‘maxLen’ – Denoting length of maximum word present in the list.
4. ‘temp’ – An array of strings to store current rectangle.
5. ‘trie’ – A data structure to efficiently search a given word.
void dfs(word, maxArea, maxLen, temp, trie)
Description of isValid function :
This function is used to check whether the current rectangle is valid or not. It will check if all elements in column-wise fashion are present in trie or not.
{bool, bool } isValid(current, trie)
O(N * (MAX ^ 2) ), where MAX is the maximum length of a word in the given list and ‘N’ is the total number of elements in the given list.
In the worst case when all words of the same length say, ‘MAX’. Then we can have a rectangle of size ‘MAX * MAX’. Our dfs function takes O(N) time as it is checking all words present in the given list. Our ‘isValid function takes O(MAX * MAX) time since the maximum area of the rectangle can be MAX * MAX in the worst case. Also inserting all words in hashMap and Trie takes O(N * MAX) time.
So overall time complexity = O(N * (MAX ^ 2) ) + O(N * MAX) = O(N * (MAX ^ 2) ).
O( N + SUM ), where ‘SUM’ is the total number of characters present in the given list and ‘N’ is the total number of elements in the given list.
Both trie and hashMap take O(SUM) space since we are inserting all characters of given list in both of them. Also, our dfs function takes O(N) space in form of recursive calls.
So overall space complexity = O(N + SUM ) + O(N) = O(N + SUM).