Skip to main content
add inverted-logic 21 byte version
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52

Regex (ECMAScript), 21 bytes

\b(x+)(xx+,|,xx+)\1\b

Try it online!

Returns a non-match if the array is insignificant, and a match otherwise (i.e., its boolean result is inverted).

If this solution were converted to match=true logic, it would be ^(?!.*\b(x+)(xx+,|,xx+)\1\b) (28 bytes).

           # No anchor; can match starting anywhere
\b         # Assert word boundary, i.e. either the start of the string before
           # an "x", a position after an "x" and before a ",", a position
           # after a "," and before an "x", or the end of the string after an
           # "x".
(x+)       # \1 = any substring of "x"es
(
    xx+,   # 2 or more "x"es followed by ","
|          # or
    ,xx+   # "," followed by 2 or more "x"es
)
\1         # Match what was captured in \1
\b         # Assert word boundary

Regex (ECMAScript), 21 bytes

\b(x+)(xx+,|,xx+)\1\b

Try it online!

Returns a non-match if the array is insignificant, and a match otherwise (i.e., its boolean result is inverted).

If this solution were converted to match=true logic, it would be ^(?!.*\b(x+)(xx+,|,xx+)\1\b) (28 bytes).

           # No anchor; can match starting anywhere
\b         # Assert word boundary, i.e. either the start of the string before
           # an "x", a position after an "x" and before a ",", a position
           # after a "," and before an "x", or the end of the string after an
           # "x".
(x+)       # \1 = any substring of "x"es
(
    xx+,   # 2 or more "x"es followed by ","
|          # or
    ,xx+   # "," followed by 2 or more "x"es
)
\1         # Match what was captured in \1
\b         # Assert word boundary
Source Link
Deadcode
  • 11.9k
  • 1
  • 66
  • 52

Regex (ECMAScript), 27 bytes

^(x(x*)(,(?=\2x?x?\b)|$))*$

Try it online!

Input is a list of positive unary numbers separated by ,. Each one is a string of xs whose length represents the number.

^
(
    x(x*)         # \2 = this element - 1
    # Assert that either the next element is in the range \2 to \2+2 inclusive,
    # or that we've reached the end and there are no more elements.
    (
        ,         # Skip to the next element
        (?=       # look ahead to the next element to make the following assertion:
            \2    # subtract \2 from it
            x?x?  # subtract any number from 0 to 2 from it
            \b    # assert that we matched the exact number
        )
    |
        $
    )
)*
$