Given a string str, the task is to validate if this string represents an Indian vehicle number.
A Number Plate consists of three parts :
Where,
UP: First two Characters denote the State Name. Basically, Initial Characters of a State is used i.e. If the vehicle is from Uttar Pradesh then,
UP Will denote the State Name.50:Next Two Numbers indicate District’s Sequential Number.
BY 1998:Next 6 (it varies from 5 to 6) letters are Unique alphanumeric string for each vehicle.
Examples:
Input: str = ”MH 05 S 9954?
Output: True
Explanation: MH – State Name
05 – District Sequential Number
S 9954 – Alphanumeric Unique CodeInput: str = ”MH 05 DL 9023 ”
Output: TrueInput: str = ”123@3459?
Output: False
Explanation: It has a unique character that is against the property of the Vehicle Number Plate code.Input: str =”9345268?
Output: False
Explanation: As Contains Number Only
Approach: The problem can be solved based on the following idea:
This problem can be dealt with regular expression. Regex will validate the entered data and will provide the exact format:
Create a regex pattern to validate the number as written below:
regex=”^[A-Z]{2}[\\ -]{0, 1}[0-9]{2}[\\ -]{0, 1}[A-Z]{1, 2}[\\ -]{0, 1}[0-9]{4}$” where^: Beginning of the String
[A-Z]: Character Set, matches a character in the range “A” to “Z”
{2}: Quantifier, Match of the two preceding items
[\\ -]{0, 1} : Matches one preceding item, It can be either space(\\) or Hyphen(-)
[0-9]{2}: Matches two preceding items, Both the item should be in the range 0 to 9
[A-Z]{1, 2}: It will match between 1 to 2, Item should be in the range “A” to “Z”
$: end of the String
Follow the steps mentioned below to implement the idea:
- Create a regex as shown above.
- Match the input string with the regex pattern.
- If the string matches the pattern then it is a valid number. Otherwise, it is not a valid number.
Below is the Code implementation of the above approach:
// C++ program to validate // Indian Vehicle Number Plate using Regular Expression #include <bits/stdc++.h> #include <regex> using namespace std;
// Function to validate the // Indian Vehicle Number Plate string isValidVehicleNumberPlate(string NUMBERPLATE) { // Regex to check valid
// Indian Vehicle Number Plate
const regex pattern(
"^[A-Z]{2}[\\ -]{0,1}[0-9]{2}[\\ "
"-]{0,1}[A-Z]{1,2}[\\ -]{0,1}[0-9]{4}$" );
// Indian Vehicle Number Plate
// is empty return false
if (NUMBERPLATE.empty()) {
return "false" ;
}
// Return true if the NUMBERPLATE
// matched the ReGex
if (regex_match(NUMBERPLATE, pattern)) {
return "true" ;
}
else {
return "false" ;
}
} // Driver Code int main()
{ // Test Case 1:
string str1 = "UP 50 BY 1998" ;
cout << isValidVehicleNumberPlate(str1) << endl;
// Test Case 2:
string str2 = "MH-05-DL-9023" ;
cout << isValidVehicleNumberPlate(str2) << endl;
// Test Case 3:
string str3 = "BNZAA2318JM" ;
cout << isValidVehicleNumberPlate(str3) << endl;
// Test Case 4:
string str4 = "MH 05 S 9954" ;
cout << isValidVehicleNumberPlate(str4) << endl;
// Test Case 5:
string str5 = "934517865" ;
cout << isValidVehicleNumberPlate(str5) << endl;
// Test Case 6:
string str6 = "MH05DL9023" ;
cout << isValidVehicleNumberPlate(str6) << endl;
return 0;
} // This code is contributed by Aman Kumar. |
// Java program to validate // Indian Vehicle Number Plate using Regular Expression import java.util.regex.*;
class GFG {
// Function to validate the
// Indian Vehicle Number Plate (For India Country Only)
public static boolean
isValidVehicleNumberPlate(String NUMBERPLATE)
{
// Regex to check valid Indian Vehicle Number Plate
String regex
= "^[A-Z]{2}[\\ -]{0,1}[0-9]{2}[\\ -]{0,1}[A-Z]{1,2}[\\ -]{0,1}[0-9]{4}$" ;
// Compile the ReGex
Pattern p = Pattern.compile(regex);
// If the Indian Vehicle Number Plate
// is empty return false
if (NUMBERPLATE == null ) {
return false ;
}
// Pattern class contains matcher() method
// to find matching between given
// Indian Vehicle Number Plate Validation using
// regular expression.
Matcher m = p.matcher(NUMBERPLATE);
// Return if the Indian Vehicle Number Plate
// matched the ReGex
return m.matches();
}
// Driver Code.
public static void main(String args[])
{
// Test Case 1:
String str1 = "UP 50 BY 1998" ;
System.out.println(isValidVehicleNumberPlate(str1));
// Test Case 2:
String str2 = "MH-05-DL-9023" ;
System.out.println(isValidVehicleNumberPlate(str2));
// Test Case 3:
String str3 = "BNZAA2318JM" ;
System.out.println(isValidVehicleNumberPlate(str3));
// Test Case 4:
String str4 = "MH 05 S 9954" ;
System.out.println(isValidVehicleNumberPlate(str4));
// Test Case 5:
String str5 = "934517865" ;
System.out.println(isValidVehicleNumberPlate(str5));
// Test Case 6:
String str6 = "MH05DL9023" ;
System.out.println(isValidVehicleNumberPlate(str6));
}
} |
# Python3 program to validate # Indian Vehicle Number Plate using Regular Expression import re
# Function to validate # Indian Vehicle Number Plate def isValidVehicleNumberPlate( str ):
# Regex to check valid Indian Vehicle Number Plate
regex = "^[A-Z]{2}[\\s-]{0,1}[0-9]{2}[\\s-]{0,1}[A-Z]{1,2}[\\s-]{0,1}[0-9]{4}$"
# Compile the ReGex
p = re. compile (regex)
# If the string is empty
# return false
if ( str = = None ):
return False
# Return if the string
# matched the ReGex
if (re.search(p, str )):
return True
else :
return False
# Driver code # Test Case 1: str1 = "UP 50 BY 1998"
print (isValidVehicleNumberPlate(str1))
# Test Case 2: str2 = "MH-05-DL-9023"
print (isValidVehicleNumberPlate(str2))
# Test Case 3: str3 = "BNZAA2318JM"
print (isValidVehicleNumberPlate(str3))
# Test Case 4: str4 = "MH 05 S 9954"
print (isValidVehicleNumberPlate(str4))
# Test Case 5: str5 = "934517865"
print (isValidVehicleNumberPlate(str5))
# Test Case 6: str6 = "MH05DL9023"
print (isValidVehicleNumberPlate(str6))
|
// C# program to validate // Indian Vehicle Number Plate using Regular Expression using System;
using System.Text.RegularExpressions;
class GFG {
// Function to validate the
// Indian Vehicle Number Plate (For India Country Only)
public static bool
isValidVehicleNumberPlate(String NUMBERPLATE)
{
// Regex to check valid Indian Vehicle Number Plate
string regex
= "^[A-Z]{2}[\\ -]{0,1}[0-9]{2}[\\ -]{0,1}[A-Z]{1,2}[\\ -]{0,1}[0-9]{4}$" ;
// Compile the ReGex
Regex p = new Regex(regex);
// If the Indian Vehicle Number Plate
// is empty return false
if (NUMBERPLATE == null ) {
return false ;
}
// Pattern class contains matcher() method
// to find matching between given
// Indian Vehicle Number Plate Validation using
// regular expression.
Match m = p.Match(NUMBERPLATE);
// Return if the Indian Vehicle Number Plate
// matched the ReGex
return m.Success;
}
// Driver Code.
public static void Main()
{
// Test Case 1:
string str1 = "UP 50 BY 1998" ;
Console.WriteLine(isValidVehicleNumberPlate(str1));
// Test Case 2:
string str2 = "MH-05-DL-9023" ;
Console.WriteLine(isValidVehicleNumberPlate(str2));
// Test Case 3:
string str3 = "BNZAA2318JM" ;
Console.WriteLine(isValidVehicleNumberPlate(str3));
// Test Case 4:
string str4 = "MH 05 S 9954" ;
Console.WriteLine(isValidVehicleNumberPlate(str4));
// Test Case 5:
string str5 = "934517865" ;
Console.WriteLine(isValidVehicleNumberPlate(str5));
// Test Case 6:
string str6 = "MH05DL9023" ;
Console.WriteLine(isValidVehicleNumberPlate(str6));
}
} // This code is contributed by Vaibhav nandan |
// Javascript program to validate // Indian Vehicle Number Plate using Regular Expression // Function to validate the // Indian Vehicle Number Plate function isValidVehicleNumberPlate(NUMBERPLATE) {
// Regex to check valid
// Indian Vehicle Number Plate
let regex = new RegExp(/^[A-Z]{2}[ -][0-9]{1,2}(?: [A-Z])?(?: [A-Z]*)? [0-9]{4}$/);
// Indian Vehicle Number Plate
// is empty return false
if (NUMBERPLATE == null ) {
return "false" ;
}
// Return true if the NUMBERPLATE
// matched the ReGex
if (regex.test(NUMBERPLATE) == true ) {
return "true" ;
}
else {
return "false" ;
}
} // Driver Code // Test Case 1: let str1 = "UP 50 BY 1998" ;
console.log(isValidVehicleNumberPlate(str1)); // Test Case 2: let str2 = "MH 05 DL 9023" ;
console.log(isValidVehicleNumberPlate(str2)); // Test Case 3: let str3 = "BNZAA2318JM" ;
console.log(isValidVehicleNumberPlate(str3)); // Test Case 4: let str4 = "MH 05 S 9954" ;
console.log(isValidVehicleNumberPlate(str4)); // Test Case 5: let str5 = "934517865" ;
console.log(isValidVehicleNumberPlate(str5)); // Test Case 6: let str6 = "MH 05 DL 9023" ;
console.log(isValidVehicleNumberPlate(str6)); // This code is contributed by akashish__ |
true true false true false true
Time Complexity: O(N) where N is the length of the string
Auxiliary Space: O(1)