Sort by

recency

|

360 Discussions

|

  • + 0 comments

    Scanner s = new Scanner(System.in); int t = s.nextInt();

        // Stream to read and process input pairs
        Set<String> uniquePairs = new HashSet<>();
    
        // We use a Stream to process the input and maintain uniqueness
        IntStream.range(0, t).forEach(i -> {
            String left = s.next();
            String right = s.next();
    
            // Create the two possible pair strings
            String pair1 = left + " " + right;
            String pair2 = right + " " + left;
    
            // Add the lexicographically smaller of the two pairs to the set
            uniquePairs.add(Stream.of(pair1, pair2)
                                  .min(String::compareTo) // Get the lexicographically smaller string
                                  .get());
    
            // Print the current number of unique pairs
            System.out.println(uniquePairs.size());
        });
    
  • + 0 comments

    If you got a problem with the test case 5 try with lexicographic order:

     Set<String> hs = new HashSet<>();
            for (int i = 0; i < t; i++) {
                String first = pair_left[i];
                String second = pair_right[i];
    
                if (first.compareTo(second) > 0) {
                    String temp = first;
                    first = second;
                    second = temp;
                }
    
                hs.add(first + "," + second);
                System.out.println(hs.size());
            }
    
  • + 0 comments

    Test case 5 has failed in my case.

    This is my code that I wrote:- 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();
        }
        s.close();
    
        HashSet<String> set = new HashSet(t);
        for (int i = 0; i < t; i++) {
            set.add(pair_left[i] + " " + pair_right[i]);
            System.out.println(set.size());
        }
    }
    

    }

  • + 1 comment

    Is there any error in this code as one last test case is failing?

    Set<String> mySet = new HashSet<>();
    for(int i = 0; i < t; i++) {
    	mySet.add(pair_left[i] + " " + pair_right[i]);
    	System.out.println(mySet.size());
    }
    
  • + 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());
        }