7
\$\begingroup\$

Given two non-empty 01 strings, check if there is string containing both given strings, and their first appearance end at same position.

Test cases:

01, 1 => true (01)
0, 00 => false (0 would at least come 1 character earlier)
01, 11 => false (can't meet on same position)
01, 0 => false (can't meet on same position)
11, 11 => true (11)
0101, 1 => false

The input can be encoded in whichever way you like.

As a , the output (true/false) can also be encoded using any 2 distinct, fixed values, or using your languages (optionally swapped) convention

Shortest code wins.

Note:

This is the promise checker of https://codegolf.meta.stackexchange.com/a/26193/

\$\endgroup\$
15
  • 2
    \$\begingroup\$ I suggest a test case like 0101, 1 => true as this prevents simply counting the matches. \$\endgroup\$
    – chunes
    Dec 26, 2023 at 6:22
  • 2
    \$\begingroup\$ Will input ensure that first input will always at least as long as the second one? If not, please add some testcases that first one in shorter. \$\endgroup\$
    – tsh
    Dec 26, 2023 at 7:34
  • 2
    \$\begingroup\$ @tsh Currently 5 of 6 cases with leading zero \$\endgroup\$
    – l4m2
    Dec 26, 2023 at 7:39
  • 2
    \$\begingroup\$ I'm having a really hard time understanding this, could you add like all the test cases: 0, 0 => ? 0, 1 => ? 0, 00 => false 0, 01 => false(?) 0, 10 => ? 0, 11 => ? 1, 0 => ? 1, 1 => ? 1, 00 => ? 1, 01 => ? 1, 10 => ? 1, 11 =>? mostly I can't see how 01,1 is true if 0101,1 and 01,0 are false \$\endgroup\$
    – guest4308
    Dec 26, 2023 at 13:37
  • 2
    \$\begingroup\$ @guest4308 For 01,1, take 00100 for example. 01 can be first found at 0*01*00 and the ending index is 2. 1 can be found at 00*1*00 and ending index is also 2. Since both ending index are same for both strings, this case will be found as true. But now take 0101,1. You will find that there is no such string like 00100 in the previous case that will satisfy the necessary conditions. You can try string 000101000 for instance, but notice that 0101 is found in 00*0101*000 with end index 5, while 1 is found 000*1*01000 with end index 3. \$\endgroup\$
    – Aiden Chow
    Dec 27, 2023 at 1:54

12 Answers 12

4
\$\begingroup\$

Jelly, 8 bytes

LÞṫw¥@Ƒ/

Try it online!

Ran through a lot of 10-12 byters, but eventually lucked out into this. Takes monadic input as a pair of values. (ṫw¥@Ƒ for 5 bytes dyadic if the shorter input can be assumed to be on the left.)

LÞ          Sort by length.
       /    With the shorter string on the left and the longer on the right:
      Ƒ     Is the left equal to
  ṫ  @      the right without the characters before
   w¥       the first occurrence of the left in it?
\$\endgroup\$
3
\$\begingroup\$

Uiua, 10 bytes

↥∩/<⌕⊃:⌕∩⇌

Try it!

Given two strings a and b, reverse them, evaluate "find" (a boolean array containing 1 at the starting points of appearance of b in a), and test if it is in the form of a 1 followed by a bunch of zeros (so the original strings match only at the end). Repeat it with arguments swapped and return the OR of the two results.

↥∩/<⌕⊃:⌕∩⇌    input: two strings a, b
         ∩⇌    reverse both a and b
     ⊃:⌕       reuse arguments:
       :          top: push b, a
        ⌕         below: push find(a, b)
    ⌕           take b, a from top and push find(b, a)
 ∩/<            test if each array is in the form of [1 0 0 ... 0] (APL trick)
↥               return the OR of the two
\$\endgroup\$
3
\$\begingroup\$

Haskell + hgl, 28 bytes

eq[0]<<on(ap<fa<<mNl*^maL)rv

Attempt This Online!

Explanation

  • on ... rv reverse both arguments
  • fa get the indexes of all occurrences of ...
    • mNl the shorter string
      ... in ..
    • maL the longer string
  • eq[0] check if it is equal to [0]

Tuple version, 29 bytes

eq[0]<(fa<U mNl*^U maL)<jB rv

Reflection

Argument routing is really expensive in this answer, so improvements here are mostly about argument rerouting.

  • A lot of the other answers here use a "try this or the arguments swapped" method and solve the problem with an assumed argument order. A function that does this could be useful.
  • It would be good to have some functions which parallelize mins and maxes. i.e. take two arguments and spit out a tuple placing them in order. This would make a massive difference, with a function xNl which sorts two arguments by length, the whole thing would be 21 bytes:
    eq[0]<<fa<<%(nXl.*rv)
    
  • Two tuple related functions:
    ot.*U :: (a -> b -> c) -> (a -> b -> c) -> (a, b) -> (c, c)
    
    and
    Cu<<(ot.*U) :: (a -> b -> c) -> (a -> b -> c) -> a -> b -> (c, c)
    
    would both allow you to parallelize the mins and maxes properly. They'd be generally useful for this sort of thing and without the above bullet point implemented they'd save bytes here.
  • A shortcut for U x<y exists but not one for x<U y. While the missing one not as philosophically motivated as the other both should probably exist.
  • Similarly a shortcut for jB x<y exists but not one for x<jB y. No harm in both.
\$\endgroup\$
3
\$\begingroup\$

AWK, 36 bytes

$1~$2a&&$1!~$2"."||$2~$1a&&$2!~$1"."

Attempt This Online!

This is a pattern that takes two strings on a line separated by a space and evaluates to a boolean. As a full program, it accepts multiple lines and prints only the lines that pass the test.

  • string ~ pattern and string !~ pattern are "match" and "not match" respectively, i.e. regex pattern is found/not found in the string. $1~$2a&&$1!~$2"." tests if $1 contains $2 but only as a suffix ($1 does not contain a pattern that is $2 followed by any extra char).
    • x y is string concatenation. If $2 is "0110", $2"." is "0110.", which becomes /0110./ in regex context. Since the input is numeric, $2 needs to be explicitly coerced to a string (fortunately, Awk keeps numeric inputs in its original form as a string, so this works). $2a concats $2 with a (an uninitialized variable, which evaluates to "").
  • The code is repeated twice with $1 and $2 swapped so that the test is done in both directions.
\$\endgroup\$
2
\$\begingroup\$

APL(Dyalog Unicode), 12 bytes SBCS

A⍨∨A←</∘⌽⍷⍥⌽

Try it on APLgolf!

A tacit function which takes strings on the left and right and determines whether one is a substring of the other only at the end.

Explanation

A⍨∨A←</∘⌽⍷⍥⌽­⁡​‎‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁢⁡​‎⁠⁠‎⁡⁠⁣‏⁠‏​⁡⁠⁡‌⁢⁢​‎⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁢⁣​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌­
          ⍥⌽  ⍝ ‎⁡reverse both strings
         ⍷    ⍝ ‎⁢find positions subsequences of right equal to left
        ⌽     ⍝ ‎⁣reverse
     </       ⍝ ‎⁣less than reduce
     </∘⌽     ⍝ ‎⁣checks if a Boolean vector is 1 0 0 ... 0
   A←         ⍝ ‎⁤assign this tacit function to A
  ∨           ⍝ ‎⁢⁡OR
 ⍨            ⍝ ‎⁢⁢swapped arguments of
A             ⍝ ‎⁢⁣function A 
💎

Created with the help of Luminespire.

\$\endgroup\$
0
2
\$\begingroup\$

Nekomata + -e, 10 bytes

ᶜ$ᵗ{iq=}s=

Attempt This Online!

ᶜ$ᵗ{iq=}s=
ᶜ$          Optionally swap the two inputs
  ᵗ{   }    Check that the following is not true:
    iq          a substring of the first input with the last character removed
      =         is equal to the second input
        s=  Check that the second input is a suffix of the first
\$\endgroup\$
1
\$\begingroup\$

APL (dzaima/APL), 10 bytes

A⍨∨A←</⍷⍢⌽

Try it online!

Same as my Dyalog APL answer, but we can take advantage of the Under operator to reverse before and after the .

\$\endgroup\$
1
\$\begingroup\$

Retina 0.8.2, 24 bytes

O#$`
$.&
^(.+)¶.*?\1

^$

Try it online! Takes input on separate lines but link is to test suite that splits on non-digits for convenience. Explanation:

O#$`
$.&

Sort in order of length.

^(.+)¶.*?\1

Delete the shorter string and up to the end of the first match of the shorter string in the longer string, if any.

^$

Check whether this actually removed the entire longer string.

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 18 17 bytes

⁼⁰⌈⌈⟦⌕A⮌θ⮌η⌕A⮌η⮌θ

Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - for true, nothing for false. Explanation:

     ⌕A             Find all matches of
          η         Second input
         ⮌          Reversed
        θ           In first input
       ⮌            Reversed
           ⌕A       Find all matches of
                θ   First input
               ⮌    Reversed
              η     In second input
             ⮌      Reversed
   ⌈⟦               Take the maximum
  ⌈                 Take the maximum
⁼                   Is equal to
 ⁰                  Literal integer `0`
                    Implicitly print

There are three cases:

  • Neither string is a substring of the other. In that case, both calls to FindAll return the empty list, so the maximum of the maximum is None. This therefore results in a false output.
  • Both strings are equal. In that case, both calls to FindAll return the list [0], so the maximum of the maximum is 0. This therefore results in a true output.
  • One string is a substring of the other. In that case, one FindAll will return the empty list, while the other will return a list of indices. In that case, the maximum of the maximum is the number of the characters after the end of first match, which we want to be 0 for a true output.
\$\endgroup\$
1
\$\begingroup\$

Python 3, 101 78 77 53 bytes

lambda x,y:['']==y.split(x)[1:]or['']==x.split(y)[1:]

Try it online!

-23 bytes by using a different approach

-1 byte by changing from ==2 to <3

-24 bytes thanks by @l4m2 by combining len(x.split(y)) and x.split(y)[-1] into one, then slightly changing the code.

\$\endgroup\$
4
  • \$\begingroup\$ 59 \$\endgroup\$
    – l4m2
    Dec 26, 2023 at 21:19
  • 1
    \$\begingroup\$ so short that repeating twice is shorter \$\endgroup\$
    – l4m2
    Dec 26, 2023 at 21:22
  • \$\begingroup\$ @l4m2 Thanks! I edited the answer. \$\endgroup\$
    – Fmbalbuena
    Dec 27, 2023 at 18:25
  • \$\begingroup\$ 48 \$\endgroup\$
    – l4m2
    Dec 28, 2023 at 20:09
0
\$\begingroup\$

JavaScript (Node.js), 50 bytes

a=>b=>(g=c=>/^\d*,$/.test(c.split(b)))(a)|g(b,b=a)

Try it online!

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

Perl 5 -pl, 44 bytes

$_=($b=<>)=~/$_$/*$b!~/$_.+$/+/$b$/*!/$b.+$/

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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