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();Stringx="([+-]?((90(\\.0+)?)|([1-8]?[0-9](\\.[0-9]+)?)))";Stringy="([+-]?((180(\\.0+)?)|((1[0-7][0-9]|[1-9]?[0-9])(\\.[0-9]+)?)))";Stringregex="^\\("+x+",\s"+y+"\\)$";Patternpattern=Pattern.compile(regex);for(inti=0;i<n;i++){Matchermatcher=pattern.matcher(scanner.nextLine());if(matcher.find())System.out.println("Valid");elseSystem.out.println("Invalid");}}}/*------------------Regex for x------------------------x = "( [+-]? ( ( 90 (\\.0+)? ) | ( [1-8]? [0-9] (\\.[0-9]+)? ) ) )"x is a floating number between (-90,+90)[+-]? means there may or may not be signFirst think of integer number between (-90,+90). Ignoring sign, One digit combination: [0-9] Two digit combination for 10 to 89 : [1-8][0-9] Ninty: 90That is, ( 90 | [1-8]?[0-9] ) Now think about floating points. \\. denotes original dot. If the number is 90, floating point should never cross 0. Only 90.00000 is allowed. (\\.0+)? means ".0" or ".00" or ".000" or ... For any number other than 90, Floating point can be made up of any digit. So (\\.[0-9]+) means any decimal pointWhole thing should be covered by () to avoid ambiguity while concatenating this x to the regex for (x,y).------------Regex for y------------------y = "( [+-]? ( (180 (\\.0+)?) | ( ( 1[0-7][0-9] | [1-9]?[0-9] ) (\\.[0-9]+)?) ) )"y is a floating number between (-180,+180)Integer (-180,+180) ignoring the sign: 180 1[0-7][0-9] [1-9][0-9] [0-9] So combining 180 or 1[0-7][0-9] or [1-9]?[0-9]Floating point for 180 is .0000. So (\\.0+)?)Floating point for others is (\\.[0-9]+)?)*/
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Detecting Valid Latitude and Longitude Pairs
You are viewing a single comment's thread. Return to all comments →
Java 15: