Search This Blog

Monday, September 16, 2013

hacker Rank Problem - Regex - Detect HTML Tags

Detect HTML Tags

The following is the solution to the Hacker Rank problem "Detect HTML Tags" using Java. For solutions to other Hacker Rank Problems visit my page hackerrank.
Score: 10/10
/**
 *
 */
package hackerRank;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Arun.G
 *
 */
public class DetectHTMLTags {

       /**
        * @param args
        */
       public static void printList(ArrayList<String> list) {
              String result = "";
              for (String tag : list)
                     result += ";" + tag;

              if (result.startsWith(";"))
                     result = result.substring(1);

              System.out.println(result);
       }

       public static void main(String[] args) {
              // TODO Auto-generated method stub
              Scanner sc = new Scanner(System.in);
              int no = sc.nextInt();
              ArrayList<String> list = new ArrayList<String>();
              while (no-- >= 0) {

                     String word = sc.nextLine();
                     String result = "";
                     if (!word.equals("")) {
                           String tagPattern = "<([^\">]+(?:\"[^\"]+\")*[^>]+)>";
                           Pattern pattern = Pattern.compile(tagPattern);
                           Matcher m = pattern.matcher(word);
                           while (m.find()) {
                                  result = m.group(1);
                                  System.out.println(result);
                                  //split space to get tag
                                  String[] group = result.split(" ");
                                  result = group[0];
                                  result = result.replace("/", "");// remove / since it matches /a etc
                                  //add only if not in list
                                  if (!list.contains(result))
                                         list.add(result);
                           }
                           // sort the list
                           Collections.sort(list);
                     }

              }
              // print result
              printList(list);
              // close scanner
              sc.close();
       }

}

Sunday, September 15, 2013

Hacker Rank Problem - Sorting - Insertion Sort - Part 2

Insertion Sort - Part 2
The following is the solution to the Hacker Rank Problem "Insertion Sort - Part 2" using Java. For solutions to other Hacker Rank Problems visit my page hackerrank.

Score: 1/1

/**
 * @author Arun.G
 *
 */
import java.util.*;
public class Solution {
   
       public static void insertionSort(int[] ar){

           int index =0;
           for(int i=1;i<ar.length;i++)
           {
               int number = ar[i];
               index++;
               insertionSort2(ar,index);
           }
          
}
    public static void insertionSort2(int[] ar,int index)
    {
        for(int i=0;i<=index;i++)
        {
            if(ar[i]> ar[index])
            {
                int temp=0;
                temp = ar[index];
                ar[index]=ar[i];
                ar[i]=temp;
            }
            else
            {
                continue;
            }
       
        }
       printArray(ar);
          
    }
/* Tail starts here */
static void printArray(int[] ar) {
         for(int n: ar){
            System.out.print(n+" ");
         }
           System.out.println("");
      }
      
      public static void main(String[] args) {
           Scanner in = new Scanner(System.in);
           int n = in.nextInt();
           int[] ar = new int[n];
           for(int i=0;i<n;i++){
              ar[i]=in.nextInt();
           }
           insertionSort(ar);
       }   
   }

Hacker Rank Problem - Sorting - Insertion Sort - Part 1

Insertion Sort - Part 1
The following is the solution to the Hacker Rank Problem "Insertion Sort - Part 1" using Java. Solutions to other Hacker Rank Problems are available in the following page HackerRank.

Score: 1/1
/**
 * @author Arun.G
 *
 */
/* Head ends here */
import java.util.*;
public class Solution {
      
          static void insertionSort(int[] ar) {
           
              int newvalue = ar[ar.length-1];
           for(int i=ar.length-1;i>0;i--)
           {
               if(newvalue<ar[i-1])
               {
                ar[i]=ar[i-1];
                printArray(ar);  
               }
               else
               {
                   ar[i]=newvalue;
                    printArray(ar);
                   return;
               }   
           }
           ar[0] = newvalue;
           printArray(ar);
             
       }  

/* Tail starts here */

 static void printArray(int[] ar) {
         for(int n: ar){
            System.out.print(n+" ");
         }
           System.out.println("");
      }
      
      public static void main(String[] args) {
           Scanner in = new Scanner(System.in);
           int n = in.nextInt();
           int[] ar = new int[n];
           for(int i=0;i<n;i++){
              ar[i]=in.nextInt();
           }
           insertionSort(ar);
       }   
   }

Thursday, September 12, 2013

Hacker Rank Problem - Search - Lonely Integer

Lonely Integer
The following is the solution to Hacker Rank Problem "Lonely Integer". If you are looking for other Hacker Rank Problem solutions please check the following Link

Score: 23/23
/**
 * @author Arun.G
 *
 */
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        //getting all the numbers
        for(int i=0;i<n;i++)
            arr[i]=sc.nextInt();
        //assigning 0th element as unique
        int unique=0;
        unique=arr[0];
        for(int i=1;i<arr.length;i++)
        {
           unique^=arr[i];
        }
        System.out.println(unique);
    }
}

Monday, September 9, 2013

Hacker Rank Problem - Regex - Find A Word

Find A Word

The following is the solution to Hacker Rank problem "Find A Word" using Java.  For other Hacker Rank problem solutions visit my Hacker Rank Solutions Page.

/**
 * @author Arun.G
 *
 */
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) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int no = sc.nextInt();
        String line="";
        while(no-- >=0)
            line+=" "+sc.nextLine();
        //split all the words using the regex it considers only words with _ as single word
        String[] AllWords = line.split("\\W");
        int testCases=sc.nextInt();
        while(testCases-->=0)
        {
            String word = sc.nextLine();
            int count=0;
            if(!word.equals(""))
            {
               String pattern=word;
 //See if we have a match and increase count
               for(int i=0;i<AllWords.length;i++)
               {
                   if(AllWords[i].matches(pattern))
                       count++;
               }
                   
               
                System.out.println(count);
            }
        }
    }
}

Sunday, September 1, 2013

Hacker Rank Problem - Regex - Alien Username

Alien Username

The following is the solution to the Hacker Rank problem “Alien Username" using Java.  For other Hacker Rank problem solutions visit my Hacker Rank Solutions Page.

/**
 * @author Arun.G
 *
 */
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) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int no = sc.nextInt();
        while(no-->=0)
        {
            String word = sc.nextLine();
            int count=0;
            if(!word.equals(""))
            {
                //for country
                String pattern ="^[._]{1}[0-9]{1,}[a-zA-Z]{0,}_{0,1}$";
                Pattern userNamePattern = Pattern.compile(pattern);
                Matcher m = userNamePattern.matcher(word);
                if(m.find())
                {
                   System.out.println("VALID");
                }
                else
                {
                    System.out.println("INVALID");
                }
            }
        }
    }
}


Labels