Problem Β· String
HTTP Accept-Language with Quality Scores (q-factors)
Learn this problemProblem statement
Practice sequence: Exact, prefix, and wildcard matching β Quality scores (q-factors).
This is Part 4 of the HTTP Accept-Language matcher. Each requested language may carry a numeric quality factor in the form tag;q=value, where 0 <= value <= 1. An entry without ;q= has quality 1.
Matching against supportedLanguages still uses the earlier exact, generic-language, and wildcard rules. Each supported tag receives the quality of the header entry that selected it. A supported tag is selected only once.
Return the matched supported tags in descending quality order. When qualities tie, keep their match order. Return only the tag names as a String[].
Function
parseAcceptLanguageWithQuality(acceptHeader: String, supportedLanguages: String[]) β String[]Examples
Example 1
acceptHeader = "fr-FR;q=1, fr-CA;q=0, fr;q=0.5"supportedLanguages = ["fr-FR", "fr-CA", "fr-BG"]return = ["fr-FR", "fr-BG", "fr-CA"]fr-FR matches exactly with q=1.0. The generic fr (q=0.5) matches the remaining supported French variant fr-BG. fr-CA matches exactly with q=0. Sorting by score descending: fr-FR (1.0), fr-BG (0.5), fr-CA (0).
Example 2
acceptHeader = "fr-FR;q=1, fr-CA;q=0, *;q=0.5"supportedLanguages = ["fr-FR", "fr-CA", "fr-BG", "en-US"]return = ["fr-FR", "fr-BG", "en-US", "fr-CA"]fr-FR matches with q=1.0. fr-CA matches with q=0. The wildcard *;q=0.5 matches the remaining supported tags fr-BG and en-US, both with q=0.5. Sorting by score descending and keeping match order for ties: fr-FR (1.0), then fr-BG and en-US (0.5, in match order), then fr-CA (0).
Example 3
acceptHeader = "en;q=0.8, fr;q=0.9, de;q=0.7"supportedLanguages = ["en-US", "fr-FR", "de-DE"]return = ["fr-FR", "en-US", "de-DE"]Generic en matches en-US (q=0.8), fr matches fr-FR (q=0.9), de matches de-DE (q=0.7). Sorted by score descending: fr-FR (0.9), en-US (0.8), de-DE (0.7).
Constraints
- Each header entry is
tagortag;q=value; missingqdefaults to1.0. - Matching uses exact, prefix (generic), and wildcard rules; a matched supported tag takes the matching entry's quality score.
- Sort matched tags by quality descending; ties keep match order (stable);
q=0sorts last. - Never output a supported tag twice.
- Return the tag names as a
String[].