We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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());
});
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Hashset
You are viewing a single comment's thread. Return to all comments →
Scanner s = new Scanner(System.in); int t = s.nextInt();