Hello Programmers, In this post, you will learn how to solve HackerRank Utopian Identification Number Solution. This problem is a part of the Regex HackerRank Series.
One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the Regex HackerRank Solutions using CPP, JAVA, PYTHON, JavaScript & PHP Programming Languages.

HackerRank Utopian Identification Number Solution
Problem
A new identification number is given for every Citizen of the Country Utopia and it has the following format.
- The string must begin with between 0–3 (inclusive) lowercase letters.
- Immediately following the letters, there must be a sequence of digits (0-9). The length of this segment must be between 2 and 8, both inclusive.
- Immediately following the numbers, there must be atleast 3 uppercase letters.
Your task is to find out if a given identification number is valid or not.
Input Format
The first line contains N, N lines follow each line containing an identification number.
Constraints
1 <= N <= 100
Output Format
For every identification number, please print
VALID
if the identification number is valid and print
INVALID
otherwise.
Sample Input
2
abc012333ABCDEEEE
0123AB
Sample Output
VALID
INVALID
Explanation
The first testcase is valid as it starts with 3 letters, followed by 6 integers (max of 8 and min of 2) and ends with more than 3 uppercase letters.
The second testcase is invalid as it satisfies the first (at least 0 lowercase letters) and the second condition (alteast 2 integers) but fails on the third condition.
Viewing Submissions
You can view others‘ submissions if you solve this challenge. Navigate to the challenge leaderboard.
HackerRank Utopian Identification Number Solution in Cpp
#include<iostream> using namespace std; #include<string.h> int main(){ int t; cin >> t; getchar(); while(t){ char c; int i=0,chk=0,countint=0,countlower=0,countupper=0; while((c=getchar())!='\n' && c!=EOF){ if(i==0){ if((c>='0' && c<= '9')) ++countint; else if(c>='a' && c<='z') ++countlower; else chk=1; // cout << " chk " << chk; } else if(countlower>=1 && c>='a' && c<='z'){ ++countlower; if(countlower>3) chk=1; } else if(i>0 && countint>=1 && c>='0' && c<='9'){ ++countint; if(countint>8) chk=1; } if(countint==0 && c>='0' && c<='9') ++countint; if(countint<=1 && c>='A' && c<='Z') chk=1; if(c>='A' && c<='Z' && countupper>=0){ ++countupper; } else{ if(countupper >0 && countupper<3) chk=1; } ++i; } if(countupper<3) chk=1; if(chk==1) cout << "INVALID\n"; else cout << "VALID\n"; --t; } return 0; }
HackerRank Utopian Identification Number Solution in Java
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String regex = "[a-z]{0,3}[0-9]{2,8}[A-Z]{3,}"; int N = sc.nextInt(); sc.nextLine(); for (int i=0; i<N; i++) { String s = sc.nextLine(); if (s.matches(regex)) { System.out.println("VALID"); } else { System.out.println("INVALID"); } } } }
HackerRank Utopian Identification Number Solution in Python
# Enter your code here. Read input from STDIN. Print output to STDOUT import re p = re.compile('[a-z]{,3}[0-9]{2,8}[A-Z]{3,}') N = int(raw_input()) for i in range(N): curr = raw_input() m = p.match(curr) if m: if m.end() == len(curr): print "VALID" else: print "INVALID" else: print "INVALID"
HackerRank Utopian Identification Number Solution in JavaScript
process.stdin.resume(); process.stdin.setEncoding("ascii"); process.stdin.on("data", function (input) { input = input.split('\n'); var n = parseInt(input[0]), strs = input.slice(1,n+1); for (i=0, j=strs.length; i<j; i+=1) { if (strs[i].match(/^[a-z]{0,3}\d{2,8}[A-Z]{3,}$/g)) { console.log('VALID'); } else { console.log('INVALID'); } } });
HackerRank Utopian Identification Number Solution in PHP
<?php $_fp = fopen("php://stdin", "r"); /* Enter your code here. Read input from STDIN. Print output to STDOUT */ fscanf($_fp, "%d", $m); $lines = array(); for ($i = 0; $i < $m; $i++) { $line = trim(fgets($_fp)); print (preg_match('/^[a-z]{0,3}[0-9]{2,8}[A-Z]{3,}$/', $line) ? 'VALID' : 'INVALID') . PHP_EOL; }
Disclaimer: This problem (Utopian Identification Number) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.