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.util.*;importjava.lang.*;publicclassSolution{publicstaticvoidmain(String[]args){Scannerscan=newScanner(System.in);Stringsmallest="";Stringlargest="";StringoriginalString=scan.nextLine();intsubstringLength=scan.nextInt();List<String>wordsList=newArrayList<>();// Iterate through the string, extracting substrings of the desired lengthfor(inti=0;i<=originalString.length()-substringLength;i++){Stringsubstring=originalString.substring(i,i+substringLength);wordsList.add(substring);}Collections.sort(wordsList);StringfirstWordOfFirstSentence=wordsList.get(0).split("\\s+")[0];StringlastSentence=wordsList.get(wordsList.size()-1);StringlastWordOfLastSentence=lastSentence.split("\\s+")[lastSentence.split("\\s+").length-1];System.out.println(firstWordOfFirstSentence);System.out.println(lastWordOfLastSentence);}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Substring Comparisons
You are viewing a single comment's thread. Return to all comments →
this took too long