HackerRank Valid PAN format Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Valid PAN format 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 Valid PAN format Solution
HackerRank Valid PAN format Solution

HackerRank Valid PAN format Solution


The equivalent of SSN in India is a PAN number, which is unique to each of its citizens. In any of the country’s official documents, the PAN number is listed as follows


Your task is to figure out if the PAN number is valid or not. A valid PAN number will have all its letters in uppercase and digits in the same order as listed above.

Input Format

First line contains NN lines follow, each having a PAN number.


  • 1 <= N <= 10
  • Each char is an uppercase letter, i.e., char ∈ [‘A‘, ‘Z‘].
  • Each digit lies between 0 and 9, i.e.digit ∈ [0, 9].
  • The length of the PAN number is always 10, i.e., length(PAN) = 10
  • Every character in PAN is either char or digit satisfying the above constraints.

Output Format

For every PAN number listed, print YES if it is valid and NO if it isn’t.

Sample Input


Sample Output


The first PAN number is valid since the first 5 characters are letters, the next 4 are digits and the last character is an alphabet. All letters in input is in uppercase.
The second PAN number is invalid as the fifth character is a digit as opposed to an letter.
The third PAN number contains lowercase characters. Hence invalid

Viewing Submissions

You can view others submissions if you solve this challenge. Navigate to the challenge leaderboard.

HackerRank Valid PAN format Solution in Cpp

#include <iostream>
#include <string>
using namespace std;

int main()
    int n;
    string s;

    cin >> n;
    bool IsPAN;
    for (int i = 0; i < n; ++i)
        IsPAN = true;
        for (int i = 0; i < 5; ++i)
            if (int(s[i]) < 65 || int(s[i]) > 90)
                IsPAN = false;
        for (int i = 5; i < 9; ++i)
            if (int(s[i]) < 48 || int(s[i]) > 57)
                IsPAN = false;
        if (int(s[9]) < 65 || int(s[9]) > 90)
            IsPAN = false;
        if (IsPAN)
            cout << "YES" << endl;
            cout << "NO" << endl;
    return 0;

HackerRank Valid PAN format Solution in Java

import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Solution {
    public static void main(String args[])
        Scanner reader = new Scanner(System.in);
        int nTestCases = Integer.parseInt(reader.nextLine());
        Pattern pattern = Pattern.compile("([A-Z]|[a-z]){5}[0-9]{4}([A-Z]|[a-z])");
        for( int i = 0 ; i < nTestCases ; i++)
            String input = reader.nextLine();
            Matcher matcher = pattern.matcher(input);
            if( matcher.find())
                boolean hasUppercase = !input.equals(input.toLowerCase());
                boolean hasLowercase = !input.equals(input.toUpperCase());
                if( hasLowercase && hasUppercase)


HackerRank Valid PAN format Solution in Python

n = input()
for i in xrange(n):
    PAN = raw_input()
    alp = PAN[:5]+PAN[-1]
    if(alp.isalpha() and alp.isupper() and PAN[5:-1].isdigit()):
        print "YES"
        print "NO"

HackerRank Valid PAN format Solution in JavaScript

process.stdin.on("data", function (input) {
	input = input.match(/^(.*)$/igm);
	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]{5}[0-9]{4}[A-Z]/g)) {
		} else {

HackerRank Valid PAN format Solution in PHP

$_fp = fopen("php://stdin", "r");
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
fscanf($_fp, "%d", $m);
for ($i = 0; $i < $m; $i++) {
    $line = trim(fgets($_fp));
    $search = '/^[A-Z]{5}[0-9]{4}[A-Z]$/';
    print (preg_match($search, $line) ? 'YES' : 'NO') . PHP_EOL;

Disclaimer: This problem (Valid PAN format) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.

Sharing Is Caring