Does it repeat?

20

1

A string of characters repeats if it contains two consecutive substrings that are equivalent.

For example, 2034384538452 repeats as it contains 3845 twice, consecutively.

Therefore, your challenge is to decide whether a string contains a repeating substring. You may take the input as a string or an array of characters.

You will never receive an empty input, and the length of the substring (if it exists) may be 1 or more.

I use 1 and 0 here as my truthy and falsy values, but you may use different values, as long as they are truthy and falsy in your language.

Examples:

abcab -> 0
bdefdefg -> 1
Hello, World! -> 1
pp.pp/pp -> 1
q -> 0
21020121012021020120210121020121012021012102012021020121012021020120210121020120210201210120210121020121012021020120210121020121012021012102012021020121012021012102012101202102012021012102012021020121012021020120210121020121012021012102012021020121012021020120210121020120210201210120210121020121012021020120210121020120210201210120210201202101210201210120210121020120210201210120210121020121012021020120210121020121012021012102012021020121012021020120210121020120210201210120210121020121012021020120 -> 0

(The last example was generated from the amount of ones between each zero in the Thue-Morse sequence)

Okx

Posted 2017-06-10T10:10:23.787

Reputation: 15 025

2Can I use inconsistent values, as long as they're still appropriately truthy or falsey? – Erik the Outgolfer – 2017-06-10T10:18:00.673

@EriktheOutgolfer Of course – Okx – 2017-06-10T10:22:18.533

@trichoplax I think he means consecutive subsequences of length >= 1. – Erik the Outgolfer – 2017-06-10T10:31:49.450

@EriktheOutgolfer "consecutive" was the word I missed. Thank you - it makes perfect sense now. – trichoplax – 2017-06-10T10:33:37.843

Can we output 1 for falsey and 0 for truthy instead? – user41805 – 2017-06-10T10:57:22.310

@KritixiLithos Only if it's truthy and falsy in your language. – Okx – 2017-06-10T11:01:32.187

I'm surprised this isn't a dupe. – Shaggy – 2017-06-10T11:12:58.217

@Shaggy, so am I, but it doesn't seem to be. – Gryphon – 2017-06-10T12:27:52.587

Why is the last one falsey? It contains 210 repeatedly. – spraff – 2017-06-10T14:22:18.913

@spraff But not consecutively. – Okx – 2017-06-10T14:25:15.143

I'm not sure I understand. Does subsequence mean non-empty substring? Does consecutive mean adjacent? – Dennis – 2017-06-10T14:43:15.123

@Dennis Consecutive means adjacent. Sub-sequence means non-empty sub-string. – Okx – 2017-06-10T14:58:17.237

@Gryphon, I was thinking in the broader terms of "Execute a RegEx test in your chosen language". – Shaggy – 2017-06-10T20:07:00.393

Can we assume printable ASCII? – Titus – 2017-06-10T21:15:10.263

@Titus Yes, that would make sense. – Okx – 2017-06-11T07:49:59.147

Answers

12

Retina, 6 bytes

(.+)\1

Try it online!

Positive value for truthy; zero for falsey.

How it works

Returns the number of matches of the regex /(.+)\1/g.

Leaky Nun

Posted 2017-06-10T10:10:23.787

Reputation: 45 011

10

Brachylog, 3 bytes

s~j

Try it online!

s~j
s    exists a sublist of input
 ~j  which is the result of a juxtaposition of something

Leaky Nun

Posted 2017-06-10T10:10:23.787

Reputation: 45 011

7

Jelly, 6 5 bytes

Ẇµ;"f

This is a full program. TIO can't handle the last test case without truncating it.

Try it online! (last test case truncated to 250 digits)

How it works

Ẇµ;"f  Main link. Argument: s (string)

Ẇ      Words; generate all substrings of s.
 µ     New chain. Argument: A (substring array)
  ;"   Vectorized concatenation; concatenate each substring with itself.
    f  Filter; keep "doubled" substrings that are also substrings.
       This keeps non-empty string iff the output should be truthy, producing
       non-empty output (truthy) in this case and empty output (falsy) otherwise.

Dennis

Posted 2017-06-10T10:10:23.787

Reputation: 196 637

5

Mathematica, 32 bytes

StringMatchQ[___~~x__~~x__~~___]

alephalpha

Posted 2017-06-10T10:10:23.787

Reputation: 23 988

Doesn't this require that the repeating string subsegments be adjacent? – DavidC – 2017-06-10T20:49:49.610

1@Svetlana, you are correct! I hadn't taken abcab-> 0 into account. – DavidC – 2017-06-11T11:32:26.893

1StringContainsQ[x__~~x__] and !StringFreeQ[#,x__~~x__]& are both shorter. – ngenisis – 2017-06-21T19:21:52.767

5

Java, 27 bytes

a->a.matches(".*(.+)\\1.*")

Pretty much a duplicate of the Retina answer, but there's no way Java's getting any shorter.

Nathan Merrill

Posted 2017-06-10T10:10:23.787

Reputation: 13 591

5

05AB1E, 5 bytes

Œ2×åZ

Try it online!

Outputs 1 as truthy value and 0 as falsy value

Explanation

Œ2×åZ
Π    # Substrings of input
 2×   # duplicate them (vectorized)
   å  # Is the element in the input? (vectorized)
    Z # Maximum value from list of elements

Datboi

Posted 2017-06-10T10:10:23.787

Reputation: 1 213

4

Python, 38 bytes

import re
re.compile(r'(.+)\1').search

Try it online!

Yawn, a regex. Checks if the string contains a string of one of more characters .+ followed by that same string that was just captured. The output search object is Truthy if there's at least one match, as can be checked by bool.

Using compile here saves over writing a lambda:

lambda s:re.search(r'(.+)\1',s)

Python, 54 bytes

f=lambda s:s>''and(s in(s*2)[1:-1])|f(s[1:])|f(s[:-1])

Try it online!

Searches for a substring that is composed two or more equal strings concatenated, as checked by s in(s*2)[1:-1] as in this answer. Substrings are generated recursively by choosing to cut either the first or last character. This is exponential, so it times out on the large test case.

Near misses:

f=lambda s,p='':s and(s==p)*s+f(s[1:],p+s[0])+f(s[:-1])
f=lambda s,i=1:s[i:]and(2*s[:i]in s)*s+f(s[1:])+f(s,i+1)

The first one doesn't use Python's in for checking substrings, and so could be adapted to other languages.

xnor

Posted 2017-06-10T10:10:23.787

Reputation: 115 687

4

Pyth - 10 9 8 bytes

f}+TTQ.:

Returns a list of all the repeating substrings (which if there aren't any, is an empty list, which is falsy)

Try It

Explanation:

f}+TTQ.:
      .:    # All substrings of the input (implicit):
f           # filter the list of substrings T by whether...
  +TT       # ...the concatenation of the substring with itself...
 }   Q      # ...is a substring of the input

Maria

Posted 2017-06-10T10:10:23.787

Reputation: 644

1

If you assume that the input is in quotes f}+TTQ.: works from 1 Byte less: link

– KarlKastor – 2017-06-12T07:16:17.883

3

PHP, 32 bytes

<?=preg_match('#(.+)\1#',$argn);

Try it online!

PHP, 38 bytes

<?=preg_match('#(.+)(?(1)\1)#',$argn);

Try it online!

Jörg Hülsermann

Posted 2017-06-10T10:10:23.787

Reputation: 13 026

3

Cheddar, 60 bytes

n->(|>n.len).any(i->(|>i).any(j->n.index(n.slice(j,i)*2)+1))

Try it online!

Leaky Nun

Posted 2017-06-10T10:10:23.787

Reputation: 45 011

You can golf: @.test(/(.+)\1/) – Downgoat – 2017-06-21T17:34:20.557

@Downgoat You should just submit that as another answer, really. – Leaky Nun – 2017-06-21T17:39:31.273

2

Python 3, 73 66 bytes

-7 bytes thanks to @LeakyNun

lambda s:any(2*s[j:i]in s for i in range(len(s))for j in range(i))

Try it online!

ovs

Posted 2017-06-10T10:10:23.787

Reputation: 21 408

71 bytes: f=lambda s:s and(any(s[:i]*2 in s for i in range(1,len(s)))or f(s[1:]))

– Leaky Nun – 2017-06-10T10:53:14.333

66 bytes: lambda s:any(2*s[j:i]in s for i in range(len(s))for j in range(i))

– Leaky Nun – 2017-06-10T10:55:54.597

2

Perl 6, 11 bytes

{?/(.+)$0/}

Test it

Expanded:

{        # bare block lambda with implicit parameter 「$_」

  ?      # Boolify the following
         # (need something here so it runs the regex instead of returning it)

  /      # a regex that implicitly matches against 「$_」
    (.+) # one or more characters stored in $0
     $0  # that string of characters again
  /
}

Brad Gilbert b2gills

Posted 2017-06-10T10:10:23.787

Reputation: 12 713

2

PHP, 32 bytes

<?=preg_match('#(.+)\1#',$argn);

Run as pipe with -F. Sorry Jörg, I hadn´t noticed You had posted the same.

non-regex version, 84 82 bytes

    for($s=$argn;++$e;)for($i=0;~$s[$i];)substr($s,$i,$e)==substr($s,$e+$i++,$e)&&die

exits with return code 0 for a repeat, times out (and exits with error) for none. Run as pipe with -nr.
assumes printable ASCII input; replace ~ with a& for any ASCII.

Titus

Posted 2017-06-10T10:10:23.787

Reputation: 13 814

1

Japt, 8 + 1 = 9 8 bytes

f"(.+)%1

Try it online. Outputs null for falsy, and an array containing all the repeating strings for truthy.

Explanation

 f"(.+)%1
Uf"(.+)%1" # Implicit input and string termination
Uf         # Find in the input
  "(.+)%1" #   a sequence followed by itself
 f         # and return the matched substring
           # output the return value

Luke

Posted 2017-06-10T10:10:23.787

Reputation: 4 675

Inconsistent output values are permitted so you could use è to return the number of matches and drop the flag. – Shaggy – 2017-06-10T11:11:42.027

Yeah. I could also just drop the flag to return the match itself, since no match returns null, which is falsy. – Luke – 2017-06-10T11:13:08.520

For input 00, it outputs 00. Are you sure this is truthy in Japt? – Okx – 2017-06-10T11:25:52.700

@Okx The string "00" is. – ETHproductions – 2017-06-10T17:21:12.277

@Okx; try this. The -Q flag "prettyprints" the output so you can see that it's an array containing a single string.

– Shaggy – 2017-06-12T14:53:27.380

1

JavaScript (ES6), 19 bytes

s=>/(.+)\1/.test(s)

Shaggy

Posted 2017-06-10T10:10:23.787

Reputation: 24 623

How about /(.+)\1/.test? – Luke – 2017-06-10T11:13:39.630

That's what I have, @Luke. – Shaggy – 2017-06-10T11:17:15.107

@Shaggy I believe he means /(.+)\1/.test itself as the complete submission. – Leaky Nun – 2017-06-10T12:59:22.023

@Luke /(.+)\1/.test is unbound (has no this). f=/(.+)\1/.test;f('aa') wouldn't work, for example. You would need /./.test.bind(/(.+)\1/) – Artyer – 2017-06-10T20:33:25.967

You can golf to: ::/(.+)\1/.test (15 bytes) – Downgoat – 2017-06-21T17:33:52.467

@Downgoat, could you elaborate? How would input be passed? – Shaggy – 2017-06-21T17:41:06.820

@Shaggy it's a function: let f= ::/(.+)\1/.test then you do f("goatgoat") – Downgoat – 2017-06-21T18:00:21.623

@Downgoat: Ooo, I like the potential this has. Thanks for bringing it to my attention. I'll look into in more depth when I get back to a computer in the morning. – Shaggy – 2017-06-21T18:12:42.030

@Shaggy I made a golfing post about the bind operator if you want to learn about it. It requires ES7 though so if you're using TIO you'll have to select Babel node

– Downgoat – 2017-06-21T18:13:57.267

@Downgoat: Hmm, it's not working on TIO (assuming I've entered everything correctly; haven't used TIO before) nor does it work in any browsers I currently have access to.

– Shaggy – 2017-06-22T11:50:59.760

1

Pyth, 15 bytes

.E.nm.bqNYtdd./

Try it!

KarlKastor

Posted 2017-06-10T10:10:23.787

Reputation: 2 352

1

V, 6 bytes

ø¨.«©±

Try it online!

Test Suite!

The program outputs 0 for falsey values, and a positive integer for positive values.

(Note that there was a small bug, so I had to gain 1 byte. Now after the bugfix, I will be able to replace with \x82)

Explanation

ø                     " This is a recent addition to V. This command takes in a regex
                      " and replaces the line with the number of matches of the regex
 ¨.«©±                " The compressed regex. This decompresses to \(.\+\)\1

user41805

Posted 2017-06-10T10:10:23.787

Reputation: 16 320

0

Cheddar, 16 bytes

@.test(/(.+)\1/)

This is a function. Try it online!

Downgoat

Posted 2017-06-10T10:10:23.787

Reputation: 27 116