Given a string of positive number ranging from 0 to 9, the task is to check whether the number is valid pin code or not by using a Regular Expression.
The valid pin code of India must satisfy the following conditions.
- It can be only six digits.
- It should not start with zero.
- First digit of the pin code must be from 1 to 9.
- Next five digits of the pin code may range from 0 to 9.
- It should allow only one white space, but after three digits, although this is optional.
Examples:
Input: num = “132103”
Output: true
Explanation:
The given number satisfies all the above mentioned conditions.
Input: num = “201 305”
Output: true
Explanation:
The given number satisfies all the above mentioned conditions.
Input: num = “014205”
Output: false
Explanation:
The given number start with zero, therefore it is not a valid pin code of India.
Input: num = “1473598”
Output: false
Explanation:
The given number contains seven digits, therefore it is not a valid pin code of India.
Approach: This problem can be used by using Regular Expression.
1. Get the number.
2. Create a regular expression to validate pin code of India as mentioned below:
regex = "^[1-9]{1}[0-9]{2}\\s{0, 1}[0-9]{3}$";
Where:
- ^ represents the starting of the number.
- [1-9]{1} represents the starting digit in the pin code ranging from 1 to 9.
- [0-9]{2} represents the next two digits in the pin code ranging from 0 to 9.
- \\s{0, 1} represents the white space in the pin code that can occur once or never.
- [0-9]{3} represents the last three digits in the pin code ranging from 0 to 9.
- $ represents the ending of the number.
3. Match the given number with the regex, in Java, this can be done by using Pattern.matcher().
4. Return true if the number matches with the given regex, else return false.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool isValidPinCode(string pinCode)
{
const regex pattern( "^[1-9]{1}[0-9]{2}\\s{0,1}[0-9]{3}$" );
if (pinCode.empty())
{
return false ;
}
if (regex_match(pinCode, pattern))
{
return true ;
}
else
{
return false ;
}
}
void print( bool n)
{
if (n == 0)
{
cout << "False" << endl;
}
else
{
cout << "True" << endl;
}
}
int main()
{
string num1 = "132103" ;
cout << num1 + ": " ;
print(isValidPinCode(num1));
string num2 = "201 305" ;
cout << num2 + ": " ;
print(isValidPinCode(num2));
string num3 = "014205" ;
cout << num3 + ": " ;
print(isValidPinCode(num3));
string num4 = "1473598" ;
cout << num4 + ": " ;
print(isValidPinCode(num4));
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValidPinCode(String pinCode)
{
String regex
= "^[1-9]{1}[0-9]{2}\\s{0,1}[0-9]{3}$" ;
Pattern p = Pattern.compile(regex);
if (pinCode == null ) {
return false ;
}
Matcher m = p.matcher(pinCode);
return m.matches();
}
public static void main(String args[])
{
String num1 = "132103" ;
System.out.println(
num1 + ": "
+ isValidPinCode(num1));
String num2 = "201 305" ;
System.out.println(
num2 + ": "
+ isValidPinCode(num2));
String num3 = "014205" ;
System.out.println(
num3 + ": "
+ isValidPinCode(num3));
String num4 = "1473598" ;
System.out.println(
num4 + ": "
+ isValidPinCode(num4));
}
}
|
Python3
import re
def isValidPinCode(pinCode):
regex = "^[1-9]{1}[0-9]{2}\\s{0,1}[0-9]{3}$" ;
p = re. compile (regex);
if (pinCode = = ''):
return False ;
m = re.match(p, pinCode);
if m is None :
return False
else :
return True
if __name__ = = "__main__" :
num1 = "132103" ;
print (num1, ": " , isValidPinCode(num1));
num2 = "201 305" ;
print (num2, ": " , isValidPinCode(num2));
num3 = "014205" ;
print (num3, ": " , isValidPinCode(num3));
num4 = "1473598" ;
print (num4, ": " , isValidPinCode(num4));
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static void Main( string [] args)
{
string [] str={ "132103" , "201 305" , "014205" , "1473598" };
foreach ( string s in str) {
Console.WriteLine( isValidPinCode(s) ? "true" : "false" );
}
Console.ReadKey(); }
public static bool isValidPinCode( string str)
{
string strRegex = @"^[1-9]{1}[0-9]{2}\s{0,1}[0-9]{3}$" ;
Regex re = new Regex(strRegex);
if (re.IsMatch(str))
return ( true );
else
return ( false );
}
}
|
Javascript
Javascript
function isValidPinCode(str) {
let regex = new RegExp(/^[1-9]{1}[0-9]{2}\s{0,1}[0-9]{3}$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "132103" ;
console.log(isValidPinCode(str1));
let str2 = "201 305" ;
console.log(isValidPinCode(str2));
let str3 = "014205" ;
console.log(isValidPinCode(str3));
let str4 = "1473598" ;
console.log(isValidPinCode(str4));
|
Output: 132103: true
201 305: true
014205: false
1473598: false
Time Complexity : O(1)
Space Complexity : O(1)