# Detect rotated strings

Output Yes if one string is a rotated version of the other.
Otherwise output No

Testcases

Input

CodeGolf GolfCode

Output

Yes

Input

stackexchange changestackex

Output

Yes

Input

stackexchange changestack

Output

No

Input

Hello World

Output

No
• So (abcdefAB, ABabcdef) is a "YES"? – Eelvex Mar 9 '11 at 12:06
• Should it really be a rotation or is a combination fine too? eg. what will Stackexchange Stackchangeex return? – jpjacobs Mar 9 '11 at 12:08
• @Eelvex, yes. @jpjacobs, It would return No. The rotation is a shift, like those LED scrolling signs – gnibbler Mar 9 '11 at 12:19
• Are the strings always whitespace-free and separated by whitespace? – Joey Mar 9 '11 at 15:20
• More specifically, what characters are allowed in those strings? – Joey Mar 9 '11 at 15:48

Fixed to say Yes/No and improved (91 chars)

import List;f[a,b]|aelem[x++y|x<-tails b|y<-inits b]="Yes";f _="No";main=interact$f.words original Haskell (92 chars) that says True/False import Data.List;f(a:b:_)=any(a==)$zipWith(++)(tails b)(inits b);main=interact(show.f.words)
• You can save a few characters with import List and also by using $instead of parens around the argument to interact. But your program outputs True and False instead of the required Yes and No, so that might take a few extra chars to fix. – user1011 Apr 15 '11 at 3:38 # Bash, 706559 55 characters new approach: read a b;c=$a$a;[[${c/$b/} ==$a ]]&&echo yes||echo no

Try it online!

## How it works

ಠ_ಠ 10 bytes for I/O

ɠṙ-$LÐ¡ɠeị“Yes“No - Main link. No arguments ɠ - Take a string from STDIN. Call that A Ð¡ - Repeat, collecting the results. Call that C L - Repetitions: Length of A ṙ-$              -   Link: Rotate once to the left
ɠ          - Take a string from STDIN. Call that B
e         - Is B in A? Yields 1 or 0
“Yes“No - Yield ["Yes", "No"]
ị        - 1-based index into the above list

# Husk, 14 bytes

Not sure about the rules here, Husk doesn't do IO at all. The closest alternative is a function:

!w¨Ye∫No¨€U¡ṙ1

Try it online!

### Explanation

!w¨Ye∫No¨€U¡ṙ1
¡    -- iterate the following for ever:
ṙ1  --   rotate string by 1
U     -- only keep the longest prefix with unique elements
€      -- is the argument in that list?
¨Ye∫No¨       -- compressed string: "Yes No"
w              -- split on space
!               -- modular index (1-based)

# Jelly, 16 bytes

ɠJṙ@€ɠe@ị“Yes“No

Try it online!

Many thanks to @cairdcoinheringaahing and @MrXcoder for helping me out in chat.

Explanation:

ɠ                   # The first string
ṙ@€               # Rotated by...
J                  #   The indexes of each element in the string
ɠ              # The second string
e@            # Exists in that list of strings.
# (either '0' or '1')
ị           # Indexed into the list...
“Yes“No    # ["yes", "no"]

# jq -Rn, 62 + 4 = 65 bytes

len('if[inputs|explode|sort|implode]|.[0]==.[1]then"Yes"else"No"end')

As in the accepted answer, the two strings should be on separate lines. -R is used to avoid having to quote the input strings, and -n is used to disable the default input processing because inputs takes care of that.

# K3 / Kona, 28

{:[y _in![;x]'!#x;Yes;No]}

# 05AB1E, 20 bytes

Code:

DgFÀD²Qi"Yes",q}}"No

Uses CP-1252 encoding. Try it online!

# Pyth, 18 bytes

?}zm.<QdlQ"YES""NO

Try it online

### Explanation

?}zm.<QdlQ"YES""NO
m   dlQ            For each number in [1, ..., len(first input)]...
.<Q               ... rotate the first input that many characters.
}z                   Check if the second input is in that list.
?         "YES""NO    Return "YES" or "NO" appropriately.

# C (gcc), 118 bytes

i,l,r;f(s,t)char*s,*t;{for(i=r=0,l=strlen(s);i<l;i++)!strncmp(s+i,t,l-i)&!strncmp(s,t+l-i,i)?r=3:0;puts(r+"No\0Yes");}

Try it online!

# Brachylog, 17 bytes

~c₂ᵐ↔ᵈ∧"Yes"|∧"No

Try it online!

(If the input strictly has to be whitespace-separated, add two bytes to prepend ṇ₁, but note that the input still has to be quoted. Brachylog does not have any sort of direct access to stdin.)

Takes input as a list of two strings, and outputs through the output variable. The actual logic is all in the first six bytes, which succeed if the elements of the input are rotations of each other, and fail if they are not:

ᵐ      Both strings in the input
~c₂       can be split into two pieces each
↔ᵈ    such that one string's two chunks are the other's swapped.

(↔ᵈ could just as well be pᵈ, pᵛ, or oᵛ which is what it was originally before I decided that ↔ᵈ is more to the point.)

Although it would be possible to brute force rotating one of them until it matches, the number of rotations would need to be explicitly bounded, and on top of that just the effort required to separate out the two elements of the input list would be fairly costly. Since it's very easy to brute force partitions in Brachylog, the fact that a rotation is just as much taking one chunk off one end and moving it to the other as it is doing that to single characters however many times saves a lot of bytes.

With very flexible I/O, this could be as short as 5 bytes, taking one string through the input variable and the other through the output variable: ~c₂↔c

# Stax, 11 bytes

Ä:∩ö┌┘/YJTI

Run and debug it

This program takes input on two lines.

Unpacked, ungolfed, and commented, it looks like this.

:(      get all rotations
s|#     other input is a member of rotations?
n!    "Yes"
.No     "No"
?       (a ? b : c)

Run this one