Validate whether a coupon code is well-formed according to a set of format and character rules.
Coupon Code Checker
gfgCoupon Code Validator
You are given a coupon code as a string. Determine whether it is valid according to the following rules:
- The code may contain uppercase letters, lowercase letters, digits, and hyphens
-. - It must satisfy a required structure such as allowed length, block separators, or character-class restrictions.
- Any invalid character, misplaced separator, or violation of the formatting rules makes the code invalid.
Return whether the code is valid.
Because the exact business rules can vary by company, treat this as a structured validation problem: inspect the string carefully, enforce the required format, and reject anything that breaks the rules.
Input Format
- A single string
coderepresenting the coupon code. - The exact validation rules are defined by the problem statement and may involve character checks and formatting checks.
Output Format
- Return
trueif the code satisfies all rules. - Otherwise return
false.
Constraints
1 <= code.length <= $10^{5}$- The code consists of printable ASCII characters unless otherwise specified.
- The solution should run in linear time relative to the length of the input string.
Example 1
Input
code = "ABCD-1234-EFGH"
Output
true
Explanation
The code uses only allowed characters and matches the expected block-separated format.
Example 2
Input
code = "ABCD_1234"
Output
false
Explanation
The underscore is not an allowed character, so the code is invalid.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.