Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

Java Hashset hackerrank solution

 In computer science, a set is an abstract data type that can store certain values, without any particular order, and no repeated values(Wikipedia). {1,2,3}  is an example of a set, but {1,2,2} 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 (a,b) and (c,d) are identical if a==c and b=d. That also implies (a,b) is not same as (b,a). 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 T denoting number of pairs. Each of the next T lines will contain two strings seperated by a single space.

Constraints:

  • 1<= T <=100000
  • Length of each string is atmost 5 and will consist lower case letters only.

Output Format

Print T lines. In the ith line, print number of unique pairs you have after taking ith   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.java :Without Using HashSet.

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();
       }
//logic of problem without using HashSet.
       int cnt=0;
       boolean found;

       if(t>=1 && t<=100000){
           for(int i=0;i<pair_left.length && i<pair_right.length;i++){
               found=false;
               for(int j=0;j<i;j++){
                  if(pair_left[i].equals(pair_left[j])){
                    if(pair_right[i].equals(pair_right[j])){
                      found=true; 
                      break;
                    } 
                 } //end-if
               }//end-for-1
 
            if(!found){
                cnt=cnt+1;
            }
            System.out.println(cnt); 
         } //end-for-2
     }//end-if
   }//end-method
}

Comments