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.
importjava.io.*;importjava.math.*;importjava.security.*;importjava.text.*;importjava.util.*;importjava.util.concurrent.*;importjava.util.function.*;importjava.util.regex.*;importjava.util.stream.*;importstaticjava.util.stream.Collectors.joining;importstaticjava.util.stream.Collectors.toList;classResult{/* * Complete the 'matchingStrings' function below. * * The function is expected to return an INTEGER_ARRAY. * The function accepts following parameters: * 1. STRING_ARRAY stringList * 2. STRING_ARRAY queries */publicstaticList<Integer>matchingStrings(List<String>stringList,List<String>queries){// Write your code hereInteger[]result=newInteger[queries.size()];for(inti=0;i<result.length;i++){result[i]=0;}stringList.sort(Comparator.comparingInt(String::length));for(inti=0;i<queries.size();i++){for(Stringitem:stringList){if(item.length()>queries.get(i).length())break;if(item.equals(queries.get(i))){result[i]+=1;}}}returnArrays.asList(result);}}publicclassSolution{publicstaticvoidmain(String[]args)throwsIOException{BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbufferedWriter=newBufferedWriter(newFileWriter(System.getenv("OUTPUT_PATH")));intstringListCount=Integer.parseInt(bufferedReader.readLine().trim());List<String>stringList=IntStream.range(0,stringListCount).mapToObj(i->{try{returnbufferedReader.readLine();}catch(IOExceptionex){thrownewRuntimeException(ex);}}).collect(toList());intqueriesCount=Integer.parseInt(bufferedReader.readLine().trim());List<String>queries=IntStream.range(0,queriesCount).mapToObj(i->{try{returnbufferedReader.readLine();}catch(IOExceptionex){thrownewRuntimeException(ex);}}).collect(toList());List<Integer>res=Result.matchingStrings(stringList,queries);bufferedWriter.write(res.stream().map(Object::toString).collect(joining("\n"))+"\n");bufferedReader.close();bufferedWriter.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sparse Arrays
You are viewing a single comment's thread. Return to all comments →
Java Solution