Skip to main content
Back to problems
Leetcode
Medium
Arrays
Strings
Hash Maps
Coupon Code Validator

Validate whether a coupon code is well-formed according to a set of format and character rules.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.

Coupon Code Checker

gfg
Problem Statement

Coupon Code Validator

You are given a coupon code as a string. Determine whether it is valid according to the following rules:

  1. The code may contain uppercase letters, lowercase letters, digits, and hyphens -.
  2. It must satisfy a required structure such as allowed length, block separators, or character-class restrictions.
  3. 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 code representing the coupon code.
  • The exact validation rules are defined by the problem statement and may involve character checks and formatting checks.

Output Format

  • Return true if 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.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.