Sign up ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Read two strings from stdin.
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
share|improve this question
    
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

34 Answers 34

JavaScript - 66

p=prompt,a=p(),b=p();alert(!b.match(a)&&!!b+b.match(a)?'Yes':'No')
share|improve this answer
    
1) This says True for “foo” and “foot”. 2) The output has to be “Yes” or “No”. The other participants spent great amount of code to produce the required output. Please do so. –  manatwork Oct 18 '13 at 13:46
    
Is the problem true/false or that 'foo' is not a rotation of 'foot'? –  tristin Oct 18 '13 at 13:51
    
1) Giving wrong result for “foo” and “foot”, is a bug. 2) Producing different output then required, is just deviation. Many answers fail to conform with the requirements in various degrees – some got downvoted, others not. Is up to you to decide whether you risk some downvotes or correct them. –  manatwork Oct 18 '13 at 14:27
    
Should be working now. –  tristin Oct 18 '13 at 14:38
    
Much better. It gives “Yes” for “foo” and “.*”, but as the question owner still not replied to the comment about allowed characters, let us suppose that only the character classed used in the samples are allowed. –  manatwork Oct 18 '13 at 15:01

Python, 66 63

a,b=raw_input().split()
print'YNeos'[a!=(2*a).replace(b,"")::2]

Another solution in 69 char

a,b=raw_input().split()
print['No','Yes'][a in b*2and len(a)==len(b)]
share|improve this answer
1  
print'YNeos'[a!=(2*a).replace(b,"")::2] –  gnibbler Oct 19 '13 at 10:05
    
@gnibbler nice trick, thanks for suggestion. I updated the code –  Coding man Oct 19 '13 at 11:26

GolfScript, 25 bytes

' '/~.2*@/''+='Yes''No'if

How it works

             # STACK: "CodeGolf GolfCode"
' '/         # Split input string by spaces.
             # STACK: [ "CodeGolf" "GolfCode" ]
~            # Dump the array.
             # STACK: "CodeGolf" "GolfCode"
.            # Duplicate the topmost string.
             # STACK: "CodeGolf" "GolfCode" "GolfCode"
2*           # Repeat the topmost string.
             # STACK: "CodeGolf" "GolfCode" "GolfCodeGolfCode"
@            # Rotate the three topmost strings.
             # STACK: "GolfCode" "GolfCodeGolfCode" "CodeGolf"
/            # Split the second topmost string around the topmost one.
             # STACK: "GolfCode" [ "Golf" "Code" ]
''+          # Flatten the array of strings.
             # STACK: "GolfCode" "GolfCode"
=            # Check for equality.
             # STACK: 1
'Yes''No'if  # Push 'Yes' for 1, 'No' for 0.
             # STACK: "Yes"
share|improve this answer

K3 / Kona, 28

{:[y _in![;x]'!#x;`Yes;`No]}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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