Given a string str, the duty is to validate if this string represents an Indian automobile quantity.
A Quantity Plate consists of three elements :
![]()
Common Indian Automobile Quantity Plate
The place,
UP: First two Characters denote the State Title. Mainly, Preliminary Characters of a State is used i.e. If the automobile is from Uttar Pradesh then,
UP Will denote the State Title.50:Subsequent Two Numbers point out District’s Sequential Quantity.
BY 1998:Subsequent 6 (it varies from 5 to six) letters are Distinctive alphanumeric string for every automobile.
Examples:
Enter: str = ”MH 05 S 9954″
Output: True
Rationalization: MH – State Title
05 – District Sequential Quantity
S 9954 – Alphanumeric Distinctive CodeEnter: str = ”MH 05 DL 9023 ”
Output: TrueEnter: str = ”123@3459″
Output: False
Rationalization: It has a singular character that’s in opposition to the property of the Automobile Quantity Plate code.Enter: str =”9345268″
Output: False
Rationalization: As Incorporates Quantity Solely
Strategy: The issue could be solved primarily based on the next thought:
This downside could be handled with common expression. Regex will validate the entered knowledge and can present the precise format:
Create a regex sample to validate the quantity as written beneath:
regex=”^[A-Z]{2}[ -]{0, 1}[0-9]{2}[ -]{0, 1}[A-Z]{1, 2}[ -]{0, 1}[0-9]{4}$” the place^: Starting of the String
[A-Z]: Character Set, matches a personality within the vary “A” to “Z”
{2}: Quantifier, Match of the 2 previous objects
[ -]{0, 1} : Matches one previous merchandise, It may be both area() or Hyphen(-)
[0-9]{2}: Matches two previous objects, Each the merchandise ought to be within the vary 0 to 9
[A-Z]{1, 2}: It should match between 1 to 2, Merchandise ought to be within the vary “A” to “Z”
$: finish of the String
Comply with the steps talked about beneath to implement the thought:
- Create a regex as proven above.
- Match the enter string with the regex sample.
- If the string matches the sample then it’s a legitimate quantity. In any other case, it isn’t a legitimate quantity.
Under is the Code implementation of the above method:
C++
|
Java
|
true true false true false true
Time Complexity: O(N) the place N is the size of the string
Auxiliary House: O(1)