


‘word1’ = [“exceptional”, “coding”, “skills”]
‘word2’ = [“great”, “coding”, “talent”]
‘pairs’ = [ [“exceptional”, “good”], [“great”, “good”], [“skills”, “talent”] ]
For each word in ‘word1’, we have:
1. “exceptional” = “great”, because “exceptional” = “good” and “good” = “great”
2. “coding” = “coding”, as every word is identical to itself.
3. “skills” = “talent”, because [“skills”, “talent”] is present in ‘pairs’.
As every word in ‘word1’ is identical to the corresponding word in ‘word2’, the given sentences are identical.
1. The array ‘pairs’ can have words that are not present in ‘word1’ and ‘word2’.
2. Each pair ‘[u, v]’ in ‘pairs’ is unique, and if a pair ‘[u, v]’ is present, then there will never be a pair ‘[v, u]’.
3. You do not need to print anything; it has already been taken care of. Just implement the function.
The first line of input contains an integer ‘T’ which denotes the number of test cases. Then, the ‘T’ test cases follow.
The first line of each test case contains three integers, ‘n’, ‘m’, and ‘p’ denoting the number of elements in array ‘word1’, ‘word2’, and ‘pairs’.
The second line of each test case contains ‘n’ space-separated words denoting ‘word1’.
The third line of each test case contains ‘m’ space-separated words denoting ‘word2’.
The following ‘p’ lines of each test case contain two space-separated words denoting an element in ‘pairs’.
For more clarity, please refer to the sample inputs.
For every test case, return true if the sentences ‘word1’ and ‘word2’ are identical, else return false.
1 <= T <= 1
1 <= n, m <= 1000
0 <= p <= 2000
Each element of ‘pairs’ contains exactly two words.
Length of each word in ‘word1’, ‘word2’ and ‘pairs[i]’ is in the range [1, 20].
Value of each word in ‘word1’, ‘word2’ and ‘pairs[i]’ varies from [a, z].
Where ‘T’ is the number of test cases, ‘n’ is the number of words in ‘word1’, ‘m’ is the number of words in ‘word2’, and ‘p’ is the number of elements in ‘pairs’.
Time limit: 1 second
Consider each element ‘[u, v]’ in ‘pairs’ as an edge of an undirected graph (as ‘[u, v]’ and ‘[v, u]’ are the same). If a node ‘v’ is reachable from a node ‘u’, then the string ‘u’ and ‘v’ are identical. Now, traverse the graph using DFS (or BFS) traversal starting from ‘word1[i]’, and if we can reach the node ‘word2[i]’ then ‘word1[i]’ and ‘word2[i]’ are identical. Following is a solution using DFS traversal:
The ‘dfs(string s1, string s2)’ function:
After constructing a graph with the ‘pairs’ array as an edge, the problem reduces to finding if the node with ‘word2[i]’ is reachable from the node with ‘word2[i]’, i.e., to check if both these nodes belong to the same connected component.
Connected component: A subgraph in which each pair of nodes is connected via a path.
We can find the connected components by performing a DFS (or BFS) traversal starting from every unvisited node. Each DFS traversal will mark each node in a single connected component as visited. Here, the visited array will store the component number of each node. If two nodes have the same component number (they belong to the same component), they are connected through a path. Following is a solution using DFS traversal:
The ‘dfs(string s1, integer num)’ function:
Disjoint-set (or Union-find) is a data structure that stores a collection of disjoint (non-overlapping) sets. This data structure consists of three basic operations:
The idea is to use a Disjoint-set ‘ds’ to store all nodes in a connected component into a single set. If two nodes are in the same set, they are identical (as they are reachable from one another). Following is the implementation:
Group Points
Group Points
Group Points
Group Points
Check Equations
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Good Bad Graph
Good Bad Graph
COUNT ISLANDS