Sort by

recency

|

356 Discussions

|

  • + 0 comments

    /@Author : Rehan/ Set hashSet = new HashSet<>();

        for(int i = 0; i<t; i++){
            String pair = pair_left[i]+","+pair_right[i];
            hashSet.add(pair);
            System.out.println(hashSet.size());
        }
    
  • + 0 comments

    I think the solution has an error. In set theory, the elements are unordered, i.e. John and Anne are the same pair as Anne and John. According to test case 2, e.g. cases: kg ni and ni kg are two different pairs. In my solution, in test 2 I have 975 different pairs, in their solution there are 990.

    If you find an error, let me know. My solution:

    import java.util.HashSet; import java.util.Scanner;

    public class SetOfSets { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);

        HashSet<String> names;
        String name1;
        String name2;
    
    
        HashSet<HashSet> setOfpairsOfnames = new HashSet<>();
        int N = scanner.nextInt();
        for (int i = 0; i < N; i++) {
            names = new HashSet<>();
            name1 = scanner.next();
            names.add(name1);
            name2 = scanner.next();
            names.add(name2);
            setOfpairsOfnames.add(names);
            System.out.println(setOfpairsOfnames.size());
        }
    
    }
    

    }

  • + 0 comments

    I've looked through several solutions and I'm quite surprised. If the problem involves handling pairs and they're already split into two arrays, why combine them back into a single string? Instead, there's a more elegant approach using the SimpleEntry class. You can simply use

    Set<AbstractMap.SimpleEntry<String, String>> uniquePairs = new HashSet<>();

    to handle the pairs directly.

  • + 0 comments

    hi guys i think converting the both arrays into string and adding it into a set is much easy process

    import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;

    public class Solution {

    public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); String [] pair_left = new String[t]; String [] pair_right = new String[t];

        for (int i = 0; i < t; i++) {
            pair_left[i] = s.next();
            pair_right[i] = s.next();
        }
        Set<String> nameSet = new HashSet<>();
        for(int i =0; i<t; i++){
            String str = pair_left[i]+" "+pair_right[i];
            nameSet.add(str);
            System.out.println(nameSet.size());
        }
    }
    

    }

  • + 0 comments

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

    public class Solution {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        sc.nextLine();
        Set<String> set=new HashSet<>();
        for(int i=0;i<n;i++){
            String str1=sc.nextLine();
            set.add(str1);
           System.out.println(set.size());  
        }
    
        sc.close();
    }
    

    }