Question

In: Computer Science

Triplicates. Given three lists of N names each, devise a linearithmic algorithm to determine if there...

Triplicates. Given three lists of N names each, devise a linearithmic algorithm to determine if there is any name common to all three lists, and if so, return the first such name

Solutions

Expert Solution

Hi buddy, I can give you two solutions. 1. Using HashMap<String,Integer>. It stores the count of a certain String. If we ever encounter a string of count 3 while dealing with the third list. We will stop processing and return the string as the answer.

Java Program : (Comments have been added for your better understanding)

import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
   public static void main (String[] args) throws java.lang.Exception
   {
   //This is list 1
       ArrayList<String> l1 = new ArrayList();
      
       //I'm loading the list with the sample data
       String s1[] = new String[]{"a","b","c","d","b"};
       for(String x : s1){
       l1.add(x);
       }
      
       //This is list2 and loading the list with the sample data
       ArrayList<String> l2 = new ArrayList();
       String s2[] = new String[]{"b","d","b","c","p"};
       for(String x : s2){
       l2.add(x);
       }
      
       //This is list3 and loading the list with the sample data
       ArrayList<String> l3 = new ArrayList();
       String s3[] = new String[]{"z","q","a","b","d"};
       for(String x : s3){
       l3.add(x);
       }
      
       //Initializing the hash map
       HashMap<String,Integer> count = new HashMap();
      
       //This loop iterates through all the elements in the list 1 and assigns a count
       //of 1 to all the Strings in this list
       for(String x : l1){
       if(!count.containsKey(x)){
       count.put(x,1);
       }
       }
      
       //This loop iterates through all the elements in the list 2 and assigns a count of
       //2 If this String already exists in the map
       for(String x : l2){
       if(count.containsKey(x)){
       count.put(x,2);
       }
       }
      
       //This loop iterates through all the elements in the list 3 and Prints the first
       //String whose count in the hash map is 2 (i.e this string exists in all the three lists)
       for(String x : l3){
       if(count.containsKey(x)&&count.get(x)==2){
       System.out.println("The First String common to all the lists is : "+x);
       return;
       }
       }
   }
}

OUTPUT:

The First String common to all the lists is : b

-----------------------------------------------------------------------------------------------------------------------------------------------

2. This is a bit complex and involves another data structure called "Trie" (Suffix Tree). I would suggest you to search for Trie/Suffix tree for further information. It is very useful and important data structure.


Related Solutions

1. Given three arrays of N names each, describe an O(nlogn) algorithm to determine if there...
1. Given three arrays of N names each, describe an O(nlogn) algorithm to determine if there is any name common to all three arrays, and if so, return the first such name.? 2. Given an input array with all equal values, compare the following sorting algorithm using big-O notation. Running Time Space Complexity Merge Sort Quick Sort Heap Sort
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why
Describe an optimal algorithm for finding the integers common to two lists of n integers each....
Describe an optimal algorithm for finding the integers common to two lists of n integers each. Evaluate how long each step in your algorithm takes using Θ-notation.
For each given integer n, determine if n is a sum of two squares. (You do...
For each given integer n, determine if n is a sum of two squares. (You do not have to find the squares.) (a) n = 19 (b) n = 45 (c) n = 99 (d) n = 999 (e) n = 1000
For each given integer n, determine if n is a sum of two squares. (You do...
For each given integer n, determine if n is a sum of two squares. (You do not have to find the squares.) (a)n= 19 (b)n= 45 (c)n= 99 (d)n= 999 (e)n=1000
Suppose you are given two circularly linked lists, L and M. Create an algorithm for telling...
Suppose you are given two circularly linked lists, L and M. Create an algorithm for telling if L and M store the same sequence of elements (but perhaps with different starting points). Please provide a main() function with it in Java to test it.
The author lists three types of waste. List the three with the given definitions. Give a...
The author lists three types of waste. List the three with the given definitions. Give a real world example of each type of waste.
Implement the Banker's algorithm for deadlock avoidance, that works on a given set of N processes...
Implement the Banker's algorithm for deadlock avoidance, that works on a given set of N processes and M resource types (N<10,M<10). Use C/C++/C# or Java for the implementation, with a simple text interface, where the user enters only the name of the input file (text only). The program reads all the necessary input data from that file. The input data and result is then displayed on the screen. You may use your program to validate the example you gave in...
Write an algorithm to check equality of two link lists.(java)
Write an algorithm to check equality of two link lists.(java)
How to write Prim's Algorithm with min-Heap and adjacency Lists?
How to write Prim's Algorithm with min-Heap and adjacency Lists?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT