Given a string str, the duty is to validate if this string represents an Indian car quantity.
A Quantity Plate consists of three elements :
The place,
UP: First two Characters denote the State Identify. Principally, Preliminary Characters of a State is used i.e. If the car is from Uttar Pradesh then,
UP Will denote the State Identify.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 car.
Examples:
Enter: str = ”MH 05 S 9954″
Output: True
Rationalization: MH – State Identify
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 novel character that’s towards the property of the Car Quantity Plate code.Enter: str =”9345268″
Output: False
Rationalization: As Accommodates Quantity Solely
Strategy: The issue may be solved based mostly on the next concept:
This downside may be handled with common expression. Regex will validate the entered information and can present the precise format:
Create a regex sample to validate the quantity as written under:
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 house() or Hyphen(-)
[0-9]{2}: Matches two previous objects, Each the merchandise needs to be within the vary 0 to 9
[A-Z]{1, 2}: It’ll match between 1 to 2, Merchandise needs to be within the vary “A” to “Z”
$: finish of the String
Observe the steps talked about under 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’s not a legitimate quantity.
Under is the Code implementation of the above strategy:
C++
|
Java
|
true true false true false true
Time Complexity: O(N) the place N is the size of the string
Auxiliary Area: O(1)