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.util.regex.Pattern;importjava.util.regex.Matcher;publicclassSolution{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);intn=scanner.nextInt();scanner.nextLine();// Here only captured group is ([a-z0-9-]+(?:\\.[a-z0-9-]+)+) i.e. the required domain without www/ww2.Stringregex="https?://(?:ww[w2]\\.)?([a-z0-9-]+(?:\\.[a-z0-9-]+)+)";Patternpattern=Pattern.compile(regex);// Creating hashset to keep only unique valuesHashSet<String>hashset=newHashSet<String>();for(inti=0;i<n;i++){Matchermatcher=pattern.matcher(scanner.nextLine());while(matcher.find())hashset.add(matcher.group(1));}// Converting hashset to arraylist to use sort() ArrayList<String>arraylist=newArrayList<String>(hashset);Collections.sort(arraylist);// Use listiterator to traverse through arraylist ListIterator<String>listiterator=arraylist.listIterator();while(listiterator.hasNext()){System.out.print(listiterator.next());if(listiterator.hasNext())System.out.print(";");}}}/*> () in regex means captured groups.> If regex="(A)(B)", group() or group(0) shows the entire matched string. group(1) shows the corresponding match to pattern "A" and group(2) shows the corresponding match to pattern "B".> (?:)--Here "?:" is added after ( to mean that this is a non-captured group. means it is not stored within group(index).------------"https?://(?:ww[w2]\\.)?([a-z0-9-]+(?:\\.[a-z0-9-]+)+)"------Example: http://www.hydrogencars-now.com.uk.org/blog2/index.phpRequired domain: hydrogencars-now.com.uk.org> http> s? means s may or may not be present just once.> ://> \\. denotes the original dot.> (?:) denotes a non-captured group.> (?:ww[w2]\\.)? means non-captured group to get www/ww2 followed by dot. This may or may not be present just once.> ([a-z0-9-]+(?:\\.[a-z0-9-]+)+) means required output. that is, group(1).> This means required domain is of the form "word.word.word...." > Here each word may consists of letters, numbers or hyphen only.> [a-z0-9-]+ means this word.> (?:\\.[a-z0-9-]+)+ means the combination ".word", one or more times. that is ".word.word.word..."> \\. denotes the original dot*/
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Detect the Domain Name
You are viewing a single comment's thread. Return to all comments →
Java 15