46
\$\begingroup\$

Write a function or program that takes as its input a string and prints a truthy value if the string is a pangram (a sequence of letters containing at least one of each letter in the English alphabet) and a falsey value otherwise.

Case of letters should be ignored; If the string is abcdefghijklmnopqrstuvwXYZ, then the function should still return a truthy value. Note that the string can contain any other characters in it, so 123abcdefghijklm NOPQRSTUVWXYZ321 would return a truthy value. An empty input should return a falsey value.


Test cases

AbCdEfGhIjKlMnOpQrStUvWxYz

==> True


ACEGIKMOQSUWY
BDFHJLNPRTVXZ

==> True


public static void main(String[] args)

==> False


The quick brown fox jumped over the lazy dogs. BOING BOING BOING

==> True

This is code golf. Standard rules apply. Shortest code in bytes wins.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ Plus points if your code can check if input is a Pungram. \$\endgroup\$ – Sainan May 17 '16 at 16:43
  • 8
    \$\begingroup\$ Question name request: Did the quick brown fox jump over the lazy dog? \$\endgroup\$ – user54200 Aug 4 '16 at 11:59

84 Answers 84

26
\$\begingroup\$

Pyth, 7 bytes

L!-Grb0

Explanation:

L             lambda (implicit b:)
    rb0       Convert b to lowercase
   G          Lowercase alphabet, "abcd...z"
  -           Set difference, all elts of first that aren't in second
 !            Logical NOT (The empty string is falsey)

Try the full-program, single-line version here.

\$\endgroup\$
5
  • \$\begingroup\$ I think the shortest way to fix this for newlines in the input is to make a function: L!-Grb0. !-Grs.z0 would also work but is longer. \$\endgroup\$ – FryAmTheEggman Dec 10 '15 at 16:13
  • \$\begingroup\$ Oh, I didn't see the question updated to include \n in the string. Thanks. \$\endgroup\$ – lirtosiast Dec 10 '15 at 22:30
  • \$\begingroup\$ 6 bytes: pyth.herokuapp.com/… \$\endgroup\$ – Maltysen Dec 11 '15 at 3:56
  • \$\begingroup\$ @Maltysen While there is a (weak) consensus on allowing strings from input to be delimited by quotes, I'm not sure about this as it goes further in requiring Python string syntax. \$\endgroup\$ – lirtosiast Dec 11 '15 at 16:04
  • \$\begingroup\$ I never would have thought an alphabet built-in would be useful... \$\endgroup\$ – Cyoce Dec 12 '15 at 8:43
17
\$\begingroup\$

Perl 6, 20 bytes

'a'..'z'⊆*.lc.comb

usage:

my &code = 'a'..'z'⊆*.lc.comb;
#  the parameter is ^ there

say code '123abcdefghijklm NOPQRSTUVWXYZ321' # True
say code '123abcdefghijklm NOPQRSTUVWXY'     # False

I used the 3 byte "french" version () of U+2286 SUBSET OF OR EQUAL TO operator instead of the 4 byte "texas" version ((<=)) which would have also required an extra space in front of it.

\$\endgroup\$
0
12
\$\begingroup\$

GS2, 11 9 bytes

☺ 6ΘàB1."

Thanks to @MitchSchwartz for golfing off 2 bytes!

The source code uses the CP437 encoding. Try it online!

How it works

☺              Push 32 (code point of space).
  6            Bitwise OR.
   Θ           Make a block of these two instructions and map it over the input.
               This turns uppercase letters into their lowercase counterparts.
      à        Push the lowercase alphabet.
       B1      Swap and apply set difference.
         ."    Push the logical NOT of the length of the result.
\$\endgroup\$
2
  • \$\begingroup\$ quick block m2 (\xe9) saves 2 bytes. \$\endgroup\$ – Mitch Schwartz Dec 10 '15 at 5:24
  • \$\begingroup\$ @MitchSchwartz Oh, so that's how you use those. Thanks! \$\endgroup\$ – Dennis Dec 10 '15 at 5:29
11
\$\begingroup\$

JavaScript ES6, 51 57

Edit 6 bytes save thx @user81655

a=>new Set(a.toUpperCase().match(/[A-Z]/g)).size>25

Test snippet

F=a=>new Set(a.toUpperCase().match(/[A-Z]/g)).size>25

function update() {  O.innerHTML=F(I.value) }
I.value='qwertyuiopasdfghjklzxcvbnm';update()
input { width: 70% }
<input id=I oninput='update()'>
<pre id=O></pre>

\$\endgroup\$
8
  • \$\begingroup\$ Would a.replace(/[^A-Z]|[^a-z]/g,'') or a.replace(/[^A-Z]/gi,'') work? \$\endgroup\$ – user46167 Dec 11 '15 at 1:12
  • 2
    \$\begingroup\$ @ev3commander no. A and a must become the same character, else the set will keep them as distinct and the size will be > 26 \$\endgroup\$ – edc65 Dec 11 '15 at 6:21
  • \$\begingroup\$ What if you use the spread operator with [...a.toUpperCase().replace(/[^A-Z]/g,'')].length>25 ? \$\endgroup\$ – Scott Dec 11 '15 at 16:55
  • \$\begingroup\$ @ScottKaye obviously no. Try it with 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' \$\endgroup\$ – edc65 Dec 11 '15 at 17:35
  • 1
    \$\begingroup\$ @user81655 right, it works, great. Thanks. I should not answer comments while asleep \$\endgroup\$ – edc65 Dec 12 '15 at 11:07
10
\$\begingroup\$

R 50 ,46 39 bytes

all(sapply(letters,grepl,readline(),T))

Edit drops the need for tolower by adding ignore.case=TRUE (T)

\$\endgroup\$
2
  • \$\begingroup\$ Not too familiar with R, but shouldn't ignore.case=TRUE (T) be included in the count as well then? \$\endgroup\$ – Ruslan Dec 11 '15 at 13:30
  • 2
    \$\begingroup\$ @Ruslan It is! It is the Tas the end, thanks to argument placement matching there is no need to actually specify the name of the argument (and T is the default alias for TRUE). The code written here performs the needed action as is, without any need to add anything. \$\endgroup\$ – plannapus Dec 11 '15 at 14:29
8
\$\begingroup\$

O, 11 bytes

GQ_s{n-}dS=

Try it online.

Sadly, O does not have set difference :/

Explanation

G            Pushes the alphabet to the stack
 Q           Pushes input to the stack
  _          Converts the string to lowercase
   s         Split string into char array
    {  }d    Iterate through array
     n       Pushes current element to the stack
      -      String subtraction
         S   Pushes a blank string to the stack
          =  Equals
\$\endgroup\$
7
\$\begingroup\$

Python 2, 53 51 bytes

f=lambda s,c=65:c>90or(chr(c)in s.upper())*f(s,c+1)

Alternate solutions:

lambda s:all(chr(c)in s.upper()for c in range(65,91))

lambda s:not set(range(65,91))-set(map(ord,s.upper()))

Thanks to xnor for pointing out that sets have an <= operator, for an alternate 51:

lambda s:set(range(65,91))<=set(map(ord,s.upper()))
\$\endgroup\$
5
  • 1
    \$\begingroup\$ If I'm not mistaken, the last expression is the same as lambda s:set(range(65,91))<=set(map(ord,s.upper())), also for 51. \$\endgroup\$ – xnor Dec 10 '15 at 5:06
  • \$\begingroup\$ Python 3.5 can save bytes here: p=lambda s:{*range(65,91)}<={*map(ord,s.upper())}. By the way, I can't seem to find any rules on whether a lambda needs to be assigned (as in your first case) or not (as in your later ones). Help? \$\endgroup\$ – Tim Pederick Dec 11 '15 at 16:30
  • \$\begingroup\$ @TimPederick Naming the lambda is unnecessary unless you need to use the function elsewhere, like in the first recursive solution. \$\endgroup\$ – FryAmTheEggman Dec 11 '15 at 16:57
  • \$\begingroup\$ @TimPederick Thanks for pointing that out. I renamed my answer as Python 2 instead of just Python. You have my blessing to post that as a new answer if you want, which I think would be ok by community norms although I'm not sure. \$\endgroup\$ – Mitch Schwartz Dec 11 '15 at 17:33
  • \$\begingroup\$ @FryAmTheEggman: Thanks for clarifying. That distinction hadn't occurred to me! I've also found a meta post explaining the rule. There goes two bytes from a few things I've written... \$\endgroup\$ – Tim Pederick Dec 12 '15 at 13:51
6
\$\begingroup\$

Julia, 38 bytes

s->endof(∩('a':'z',lowercase(s)))>25

This is simple - lowercase deals with the uppercase/lowercase issue, 'a':'z' holds all of the lowercase letters, is intersection, removes any character that isn't a letter and, because 'a':'z' comes first, will only have one of each letter that appears in s. endof is the shortest way to get the length of the resulting array, and if it's 26, then it's a pangram (it can't be more than 26, and >25 saves a byte relative to ==26).

\$\endgroup\$
5
\$\begingroup\$

Retina, 22 bytes

Msi`([a-z])(?!.*\1)
26

Try it online.

The first line matches any letter which does not appear again later in the string. That ensures that we don't match each letter at most once, no matter how often it occurs. Match mode will by default replace the string with the number of matches found. So in the second stage, we match 26 against the result of the first input, which will give either 0 or 1, depending on whether we found the maximum of 26 matches or not.

\$\endgroup\$
5
\$\begingroup\$

Ruby, 41 33

->s{(?a..?z).all?{|c|s[/#{c}/i]}}

Usage

p=->s{(?a..?z).all?{|c|s[/#{c}/i]}}
p["AbCdEfGhIjKlMnOpQrStUvWxYz"] 
  #=> true
p["ACEGIKMOQSUWY
BDFHJLNPRTVXZ"]
  #=> true
p["public static void main(String[] args)"]
  #=> false
p["The quick brown fox jumped over the lazy dogs. BOING BOING BOING"]
  #=> true

Thanks to Vasu Adari for saving me 8 bytes

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You can save 8 bytes by making your regex to ignorecase. \$\endgroup\$ – Vasu Adari Dec 11 '15 at 7:49
5
\$\begingroup\$

05AB1E, 4 bytes (Probably Non-competing)

lêAå

Try it online!

l    # Push lowercase input.
 ê   # Push sorted, uniquified lowercase input.
  A  # Push lowercase alphabet.
   å # Is lowercase alphabet in sorted, uniquified, lowercase input?
     # True if panagram, false if not.
\$\endgroup\$
5
\$\begingroup\$

Haskell, 59 56 53 51 bytes

p s=and[any(`elem`map toEnum[a,a+32])s|a<-[65..90]]

Try it online!

Explanation:

Give an input string s, for each a in range 65 to 90 (the ASCII codes for A to Z) it is checked whether any character in s is equal to either a (the upper case character) or a+32 (the lower case character), converted to a character by toEnum. This generates a list of booleans. and checks if they're all True.

Old version:

import Data.Char
p s=and[any((==)a.toUpper)s|a<-['A'..'Z']]

For every upper case alphabet letter, check whether some letter from s in upper case is equal to it. any(==a)s is the same as elem a s but allows to modify the elements of s before the comparison - in this case, covert them to upper case.

\$\endgroup\$
0
5
\$\begingroup\$

Haskell, 43 bytes

f s=until(all(`notElem`s))(succ<$>)"Aa">"["

Try it online!

Iterates through the lowercase/uppercase pairs until it finds one where both are not elements of the input, and checks that this failure is past the alphabet.

\$\endgroup\$
5
\$\begingroup\$

Vim, 30 26 bytes

:%s/\A*/\r/g
:sor ui
26Dr1

Try it online!

Let's get this language of the month started! I am still a beginner in Vim, so golfing suggestions are much appreciated :)

Explanation

The first line of the program uses a regex to substitute any (possibly empty) sequence of non-alphabetic characters (\A*) with a newline; since the empty string also gets matched, this will result in a series of lines each containing at most one character, which will be a letter. The first line will always be empty (the initial empty string became a newline).

:sor ui sorts the lines ignoring case, and removes duplicates (also ignoring case). If the input was a pangram we will now have an empty line followed by 26 lines each with a character from a to z; if the input was not a pangram, some of those lines will be missing.

26D deletes the first 26 lines, which will leave us with either z (for a pangram) or an empty string (for a non-pangram). The only truthy values in Vim are non-zero numbers (or strings starting with a non-zero number), so our last step will be r1 (replace the first character with 1) which will result in 1 for a pangram, and an empty string otherwise.

\$\endgroup\$
4
\$\begingroup\$

Minkolang 0.14, 18 bytes

$o7$ZsrlZ'26'$ZN.

Try it here.

Explanation

$o                    Read in whole input as characters
  7$Z                 Uppercase every letter
     s                Sort
      r               Reverse
       lZ             Alphabet - uppercase and lowercase
         '26'         Pushes 26 on the stack
             0$Z      Count how often the top 26 numbers of the stack appear in the stack
                N.    Output as number and stop.
\$\endgroup\$
4
\$\begingroup\$

Python 3.5, 47 bytes

lambda s:{*map(chr,range(65,91))}<={*s.upper()}

Same principle as Mitch Schwartz's answer, but using the PEP 0448 enhancements to * unpacking, first introduced in Python 3.5.

This version differs slightly from what I wrote in my comment to Mitch's post, in that I turn the numbers into letters rather than vice versa. That's because that's how I wrote my original attempts at a solution, before discovering that I couldn't out-golf Mitch without outright copying his approach. So consider that tweak my one remaining shred of originality!

\$\endgroup\$
4
\$\begingroup\$

R, 53 45 bytes

all(97:122%in%utf8ToInt(tolower(readline())))

Old version at 53 bytes:

all(letters%in%strsplit(tolower(readline()),"")[[1]])

Usage:

> all(97:122%in%utf8ToInt(tolower(readline())))
The quick brown fox jumps over the lazy dog
[1] TRUE
> all(97:122%in%utf8ToInt(tolower(readline())))
Write a function or program that takes as its input a string and prints a truthy value if the string is a pangram and a falsey value otherwise.
[1] FALSE
> all(97:122%in%utf8ToInt(tolower(readline())))
123abcdefghijklm NOPQRSTUVWXYZ321
[1] TRUE
> all(97:122%in%utf8ToInt(tolower(readline())))
Portez ce vieux whisky au juge blond qui fume
[1] TRUE
\$\endgroup\$
4
\$\begingroup\$

MATLAB / Octave, 35 33 bytes

@(x)~nnz(setdiff(65:90,upper(x)))

Try it online!


The anonymous function returns a logical 1 if the input x is a pangram, or a logical 0 if it isn't.

Essentially it uses the same approach as @ThomasKwa's Pyth solution. The set difference between all characters in the upper case alphabet range (65:91) and the input string (converted to upper case). Any characters that are in the alphabet but not in the input string are returned by setdiff. Only if the array returned by the set difference is empty is the string a pangram.

Using upper case instead of lower case saves a couple of bytes compared with 'a':'z' because the ASCII value can be used instead to make the range.

\$\endgroup\$
1
  • \$\begingroup\$ Great answer! Mine was 10 bytes longer \$\endgroup\$ – Luis Mendo Dec 10 '15 at 23:37
4
\$\begingroup\$

Brachylog, 4 bytes

ḷo⊇Ạ

Try it online!

        The input
ḷ       lowercased
 o      sorted
  ⊇     is a superlist of
   Ạ    the lowercase alphabet.
\$\endgroup\$
3
\$\begingroup\$

Japt, 14 bytes

#ao#{ e@Uv fXd

Try it online!

How it works

        // Implicit: U = input string
#ao#{   // Generate a range of integers from charCode("a") to charCode("{").
e@      // Check if every item X in this range returns truthily to:
Uv fXd  //  convert U to lowercase, and put all instances of X.toCharCode() in an array.
        // This returns false if U does not contain one of the characters.
        // Implicit: output last expression
\$\endgroup\$
3
\$\begingroup\$

CJam, 11 bytes

'[,65>qeu-!

This is a complete program. Try it online.

Explanation:

'[,65>  Build upper case alphabet (see CJam tips thread).
q       Get input.
eu      Convert to all upper case.
-       Set difference between alphabet and upper cased input.
!       Negate.
\$\endgroup\$
3
\$\begingroup\$

Bash, 45 42 bytes

41 byte program, plus 1 because it must be invoked with bash -e:

for i in {a..z}
{ [ ${1//[^$i${i^}]} ]
}

Amazingly, I managed a Bash answer with no quote characters! (yes, I checked with inputs beginning with -f and the like).

This assumes a locale where the lower-case English letters are contiguous from a to z. Input is via the first argument to the program.

The way this works is, for each alphabetic letter $i, we test whether the string contains $i or its upper-case equivalent ${i^} by removing all other characters. If this results in the empty string, then the input did not contain that letter, and we exit with 1 (false). If we have a non-empty result, then we passed the test and move on to the next letter. If the input string contains every English letter, we will reach the end of the program, thus exiting with 0 (true).

\$\endgroup\$
1
  • \$\begingroup\$ Very nice. Less golfy but kinda fun alternative: comm -23 <(printf %s\\n {A..Z}) <(sort <(printf %s\\n ${1^^})) has no output iff true. Try it online! \$\endgroup\$ – Jonah Mar 29 at 2:48
3
\$\begingroup\$

Javascript, 110 109 99 95 93 bytes

a=prompt(b=0).toUpperCase();for(i=65;i++<91;)b+=!~a.indexOf(String.fromCharCode(i));alert(!b)

Saved 6 bytes thanks to Thomas Kwa, and 10 thanks in part to ev3.

\$\endgroup\$
4
  • \$\begingroup\$ Would b=0 work for b=[]? \$\endgroup\$ – user46167 Dec 11 '15 at 1:13
  • \$\begingroup\$ Not with this approach. But I may be able to make that work. \$\endgroup\$ – SuperJedi224 Dec 11 '15 at 1:32
  • \$\begingroup\$ I don't know Javascript, but can you do for(i=65;i++<91;)b+=!~a.indexOf(String.fromCharCode(i));alert(!b)? \$\endgroup\$ – lirtosiast Dec 11 '15 at 1:40
  • \$\begingroup\$ Wow. That's even shorter than what I just did. \$\endgroup\$ – SuperJedi224 Dec 11 '15 at 1:50
3
\$\begingroup\$

PowerShell v3+, 65 56 52 Bytes

($args.ToLower()-split''|sls [a-z]|group).Count-eq26

Thanks to TessellatingHeckler for the 9-byte golf.

  • Takes the input string, converts it .ToLower()case, then -splits on every character
  • Those are fed into an alias sls for Select-String which matches based on a regex [a-z] to pull out only the letters
  • Those are then fed into Group-Object, so we're only selecting one individual instance of each letter
  • That is then .Counted to see if it's -equal to 26, and prints True or False accordingly
  • Requires PowerShell v3 or newer for the sls alias
\$\endgroup\$
2
  • \$\begingroup\$ It's a lot easier to pinch someone else's effort and try and shorten it a bit, than to read the questions from scratch - and more fun to compete within PowerShell than to go directly up against CJam and friends. But I can stop chasing your answers if it's annoying, sorry. \$\endgroup\$ – TessellatingHeckler Dec 15 '15 at 16:58
  • \$\begingroup\$ @TessellatingHeckler Not annoying! :D I'm just trying to give you more encouragement! \$\endgroup\$ – AdmBorkBork Dec 15 '15 at 18:00
3
\$\begingroup\$

2sable, 6 5 bytes

6 byte version:

AIl-g_

Try it online!

Explanation:

A        Push alphabet
 Il      Push lowercase input
   -     Remove all chars of input from alphabet
    g    Get length of the remainder
     _   Print negative bool, where length < 1 = 1 (true), length > 0 = 0 (false)

5 byte version, inspired by carusocomputing's 05AB1E answer:

lÙ{Aå

Try it online!

Explanation:

l        Push lowercase input
 Ù{      Push sorted uniquified input
   A     Push alphabet
    å    Is alphabet in sorted, uniquified input?
\$\endgroup\$
3
\$\begingroup\$

J, 23 bytes

(u:65+i.26)*/@e.toupper

Try it online!

\$\endgroup\$
3
+100
\$\begingroup\$

APL (Dyalog Extended), 6 bytes

∧/⎕A∊⌈

Try it online!

Explanation:

∧/⎕A∊⌈  ⍝ Monadic function train
      ⌈  ⍝ Convert the input to uppercase
  ⎕A∊   ⍝ For each item in the set of uppercase
         ⍝ letters, determine if it exists in the
         ⍝ uppercased input
∧/       ⍝ And-reduce: test if all letters exist
\$\endgroup\$
3
\$\begingroup\$

Retina, 20 bytes

T`L`l
D`.
C`[a-z]
26

Try it online!

Explained

T`L`l        Transliterate stage. Replaces uppercase letters with lowercase letters.
D`.          Deduplicate stage - keep one copy of every match (meaning every character in this case), discarding suplicates.
C`[a-z]      Count stage - Count lowercase letters
26           (implicit) Count stage - Match the string "26"
             (Since there can't be duplicates, a match means each character occurred once.)
             (output the result of the last stage - 1 for pangrams, 0 for non-pangrams)
\$\endgroup\$
3
\$\begingroup\$

APL (Dyalog Extended), 1̶8̶ 1̶4̶ 12 bytes

{⎕A(∧/∊)⌈⍕⍵}
 ⎕A(∧/∊) ⍝ Check membership in Alphabet array and bitwise reduce by logical AND.
        ⌈ ⍝ Convert to uppercase
         ⍕⍵ ⍝ Encode as character array (handles matrices, empty sets and integers)

Edit: Changed 1⎕C to thanks to rak1507

Edit: Changed 1(819⌶) to 1⎕C thanks to Razetime.

Try it online!

\$\endgroup\$
6
  • \$\begingroup\$ I think ⎕C can be used in the latest version instead of 819⌶ \$\endgroup\$ – Razetime Mar 29 at 6:28
  • \$\begingroup\$ @Razetime Thanks! I didn't know about that. \$\endgroup\$ – Andrew Ogden Mar 29 at 14:09
  • \$\begingroup\$ ⎕A≡⎕A∩⌈ using ⌈ from extended to uppercase \$\endgroup\$ – rak1507 Apr 1 at 8:18
  • \$\begingroup\$ @rak1507 Very cool solution, but I think it fails the second test case. \$\endgroup\$ – Andrew Ogden Apr 1 at 23:57
  • \$\begingroup\$ You don't need to support matrices \$\endgroup\$ – rak1507 Apr 2 at 11:59
3
\$\begingroup\$

All of these regexes use their engines' respective case insensitivity flags, so that has not been counted towards the byte counts. Even though some use \pL (a shorthand for \p{L}) instead of [A-Z], they still need the flag, due to comparing characters via backreference.

Regex (Perl 5 / PCRE / Boost / Pythonregex), 21🐌, 22🐌, 24, or 25 bytes

Quite simply, this regex works by asserting that there are at least 26 alphabetical characters in the string that do not match with any character to the right of themselves. Each of these characters will be the last occurrence of a given letter of the alphabet in the string; as such, they are all guaranteed to be different letters, and asserting that there are 26 of them implies that the full set A-Z is covered.

(.*(\pL)(?!.*\2)){26} (21 bytes) 🐌 - Try it online! (Perl 5)

Using \pL instead of [A-Z] to match alphabetical characters imposes the requirement that the input must be in ASCII, because \pL matches all Unicode alphabetical characters (including those that are extended ASCII in common codepages); so an accented letter, for example, would count towards consuming the target of 26 loop iterations. Some other regex engines only support this syntax in the form of \p{L} (which offers no advantage over [A-Z] for this particular problem, being equal in length) and not with the shortened syntax of \pL.

This bare-bones version of the regex is extremely slow, due to an excessively huge amount of backtracking both for pangrams and non-pangrams, but will always give the correct result when given enough time. Its search for the 26 matches proceeds in the most pessimal way possible.

Using a lazy quantifier speeds it up greatly when matching pangrams, but it's still incredibly slow to yield non-matches when given non-pangrams:

(.*?(\pL)(?!.*\2)){26} (22 bytes) 🐌

Try it online! (Perl 5)
Try it online! (PCRE2 / C++, with backtrack limit adjusted)
Try it online! (PCRE2 / PHP, with backtrack limit adjusted)

Changing the main loop to an atomic group allows the regex to also yield non-matches at a reasonable speed:

(?>.*?(\pL)(?!.*\1)){26} (24 bytes)

Try it on regex101 (PCRE1)
Try it online! (Python import regex)

Adding an anchor is not necessary to make the regex yield correct results, but speeds up non-matches further, by preventing the regex engine from continuing to try for a match at every character of the string (because if it fails to match at the beginning, we know it's guaranteed not to match anywhere in the same string, but the regex engine can't know that):

^(?>.*?(\pL)(?!.*\1)){26} (25 bytes)

Try it on regex101 (PCRE1)
Try it online! (Python import regex)
Try it online! (Perl 5)
Try it online! (PCRE2 / C++)
Try it online! (PCRE2 / PHP)
Try it online! (Boost / C++)

Regex (.NET / Java / Ruby), 23🐌, 24🐌, 26, or 27 bytes

These regex engines don't support \pL (they support \p{L}, but that isn't useful when we already need the case insensitivity flag anyway), thus [A-Z] is used:

(.*([A-Z])(?!.*\2)){26} (23 bytes) 🐌
(.*?([A-Z])(?!.*\2)){26} (24 bytes) 🐌
(?>.*?([A-Z])(?!.*\1)){26} (26 bytes)
^(?>.*?([A-Z])(?!.*\1)){26} (27 bytes)

^            # 1. Anchor to the start of the string (without this, the regex would still
             #    work but would be slower in its non-matches, due to trying to match at
             #    every character position in the string, which we know will fail, but
             #    the regex engine can't know)
(?>          # 2. Start an atomic group (every complete iteration of matching this group
             #    is set in stone, and will not be backtracked); without only a normal
             #    group, the regex would still work, but with most non-pangram inputs,
             #    would take longer than the age of the universe to yield a non-match,
             #    due to trying every way of matching ".*?" at every iteration of the loop
    .*?      # 3. Skip zero or more characters, as few as possible in order to make the
             #    following match
    ([A-Z])  # 4. Capture and consume an alphabetical character in \1
    (?!      # 5. Negative lookahead: Match outside (with zero-width) only if the inside
             #    does not match
        .*   # 6. Skip forward by zero characters or more in an attempt to make the
             #    following expresison match
        \1   # 7. Match the character captured in \1
    )        # 8. The effect of this negative lookahead is to assert that at no point
             #    right of where \1 was captured does any character match \1
){26}        # 9. Only match if this group can be matched exactly 26 times in a row,
             #    i.e. if we can find 26 characters in the range A-Z that don't match
             #    any character right of themselves, i.e. that we can find the last
             #    occurrence of 26 different alphabetical character in the string.

Try it online! (.NET / C#)
Try it online! (Java)
Try it online! (Ruby)

Regex (ECMAScript / Python), 23🐌, 24🐌, 32, or 33 bytes

The same progression of speed applies:

(.*([A-Z])(?!.*\2)){26} (23 bytes) 🐌 - Try it online! (ECMAScript) / (Python)
(.*?([A-Z])(?!.*\2)){26} (24 bytes) 🐌 - Try it online! (ECMAScript) / (Python)

ECMAScript and Python lack atomic groups, so they must be emulated using lookahead+capture+backref:

((?=(.*?([A-Z])(?!.*\3)))\2){26} (32 bytes) - Try it online! (ECMAScript) / (Python)
^((?=(.*?([A-Z])(?!.*\3)))\2){26} (33 bytes) - Try it online! (ECMAScript) / (Python)


Edit: Silly me, I assumed that this problem required variable-length lookbehind or other tricks that substitute for it, without even trying to do it without that. Thanks to @Neil for pointing this out.

\$\endgroup\$
7
  • \$\begingroup\$ You can save a byte by using positive lookahead instead of negative. This matches the last of each letter rather than the first, but the overall result is the same, excecpt of course that it works in less powerful regex engines such as ES6. \$\endgroup\$ – Neil Dec 29 '19 at 11:25
  • \$\begingroup\$ Try it online! \$\endgroup\$ – Neil Dec 29 '19 at 11:25
  • \$\begingroup\$ @Neil Oops! How embarrassing. Thanks for letting me know. (A shame, since I thought this problem was even better than Do you make me up? for illustrating the differences between engines.) I guess I'm guilty here of "When you have a hammer, every problem looks like a nail." I'll try to avoid that pitfall in the future. \$\endgroup\$ – Deadcode Dec 30 '19 at 3:33
  • \$\begingroup\$ Downvoting this because these (at least the Perl ones) are snippets, not programs or functions. They are just the regex pattern that needs to be incorporated into a program. \$\endgroup\$ – Xcali Jan 19 at 16:38
  • \$\begingroup\$ @Xcali They are full programs with Regex as the language. I never claim anywhere in this answer that they are programs in Perl, Ruby, Java, etc. They are programs in various flavors of Regex, with the regex engine of Perl, for example, being the language in use, not Perl itself. \$\endgroup\$ – Deadcode Jan 19 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.