HackerRank Valid PAN format Solution

Hello Programmers, In this post, you will know how to solve the HackerRank Valid PAN format Solution. This problem is a part of the Regex HackerRank Series.

HackerRank Valid PAN format Solution
HackerRank Valid PAN format Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Valid PAN format Solution

Problem

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

<char><char><char><char><char><digit><digit><digit><digit><char>

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.

Constraints

  • 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

3
ABCDS1234Y
ABAB12345Y
avCDS1234Y

Sample Output

YES
NO
NO

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.

Ezoicreport this adHackerRank Valid PAN format Solutions in Cpp

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int n;
    string s;
    cin >> n;
    cin.ignore();
    
    bool IsPAN;
    for (int i = 0; i < n; ++i)
    {
        getline(cin,s);
        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;
        else
            cout << "NO" << endl;
           
    }
    return 0;
}

HackerRank Valid PAN format Solutions 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)
                    System.out.println("NO");
                else
                    System.out.println("YES");
            }
            else
                System.out.println("NO");
        }
    }
}
Ezoicreport this ad

HackerRank Valid PAN format Solutions 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"
    else:
        print "NO"

HackerRank Valid PAN format Solutions in JavaScript

process.stdin.resume();
process.stdin.setEncoding("ascii");
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)) {
			process.stdout.write('YES\n');
		} else {
			process.stdout.write('NO\n');
		}
	}
});

HackerRank Valid PAN format Solutions in PHP

<?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 BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: Find HackerRank Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad