You are viewing a single comment's thread. Return to all comments →
JAVA SOLUTION import java.io.; import java.util.;
public class Solution {
public static void regex(String ip){ String [] ipParts = ip.split("[.]"); if (ipParts.length != 4) { System.out.println("false"); return; } for (String string : ipParts) { if(string.length() > 3) { System.out.println("false"); return; } else if(string.length() <= 2 && !string.matches("^[0-9][0-9]{0,3}$")){ System.out.println("false"); return; } else if((string.length() == 3 && string.charAt(1) == '5' ) && !string.matches("^[0-2][0-5][0-5]{0,3}$")){ System.out.println("false"); return; } else if(string.length() == 3 && !string.matches("^[0-2][0-5][0-9]{0,3}$")){ System.out.println("false"); return; } } System.out.println("true"); } public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String ip = sc.nextLine(); regex(ip); } }
}
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Java Regex
You are viewing a single comment's thread. Return to all comments →
JAVA SOLUTION import java.io.; import java.util.;
public class Solution {
}