Wednesday, July 26, 2023

Java Hashset

 In computer science, a set is an abstract data type that can store certain values, without any particular order, and no repeated values(Wikipedia).  is an example of a set, but  is not a set. Today you will learn how to use sets in java by solving this problem.

You are given  pairs of strings. Two pairs  and  are identical if  and . That also implies  is not same as . After taking each pair as input, you need to print number of unique pairs you currently have.

Complete the code in the editor to solve this problem.

Input Format

In the first line, there will be an integer  denoting number of pairs. Each of the next  lines will contain two strings seperated by a single space.

Constraints:

  • Length of each string is atmost  and will consist lower case letters only.

Output Format

Print  lines. In the  line, print number of unique pairs you have after taking  pair as input.

Sample Input

5
john tom
john mary
john tom
mary anna
mary anna

Sample Output

1
2
2
3
3

Explanation

  • After taking the first input, you have only one pair: (john,tom)
  • After taking the second input, you have two pairs: (john, tom) and (john, mary)
  • After taking the third input, you still have two unique pairs.
  • After taking the fourth input, you have three unique pairs: (john,tom), (john, mary) and (mary, anna)
  • After taking the fifth input, you still have three unique pairs.

Solution:

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


class Pair<A, B> {
    private A first;
    private B second;

    public Pair(A first, B second) {
        super();
        this.first = first;
        this.second = second;
    }

    public int hashCode() {
        int hashFirst = first != null ? first.hashCode() : 0;
        int hashSecond = second != null ? second.hashCode() : 0;

        return (hashFirst + hashSecond) * hashSecond + hashFirst;
    }

    public boolean equals(Object other) {
        if (other instanceof Pair) {
            Pair otherPair = (Pair) other;
            return 
            ((  this.first == otherPair.first ||
                ( this.first != null && otherPair.first != null &&
                  this.first.equals(otherPair.first))) &&
             (    this.second == otherPair.second ||
                ( this.second != null && otherPair.second != null &&
                  this.second.equals(otherPair.second))) );
        }

        return false;
    }

    public String toString()
    { 
           return "(" + first + ", " + second + ")"
    }

    public A getFirst() {
        return first;
    }

    public void setFirst(A first) {
        this.first = first;
    }

    public B getSecond() {
        return second;
    }

    public void setSecond(B second) {
        this.second = second;
    }
}
public class Solution {

    static private final String INPUT = "in";  
    static private final String OUTPUT = "out";  
  
    public static void main(String[] args) 
    {
        
      FileInputStream instream = null;  
      PrintStream outstream = null;  
     
      try {  
          instream = new FileInputStream(INPUT);  
          outstream = new PrintStream(new FileOutputStream(OUTPUT));   
      } catch (Exception e) {  
          System.err.println("Error Occurred.");  
      }  
        Set setA = new HashSet();
        Scanner scan = new Scanner(System.in);
        int t=scan.nextInt();
        for(int i=0;i<t;i++)
        {
            String a=scan.next();
            String b=scan.next();
            Pair P=new Pair(a,b);
            setA.add(P);
            System.out.println(setA.size());
            
        }
            
        
    }
}

No comments:

Post a Comment

Featured Post

14. Longest Common Prefix

Popular Posts