Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Saturday, April 1, 2017

First non-repeating character geeks solution in java




import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
 {
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t>0)
{
   int n=ab.nextInt();
   int arr[]=new int[500];
   int order[]=new int[26];
   int p=0;
   while(p<n)
   {
       arr[p]=(int)(ab.next().charAt(0)-97); //to hold the order
       order[arr[p]]++;//to couunt occurence of order
       int i=0;
       boolean flag=false;
       while(i<=p)
       {
           if(order[arr[i]]==1)
           {
           System.out.print(Character.toString ((char) (arr[i]+97))+" ");
           flag=true;
               break;
           }
           i++;
       }
       if(!flag)
       System.out.print("-1 ");
       p++;
   }
   t--;
   System.out.println("");
}
}
}

0 Comments:

Post a Comment

Stats