[CHALLENGE] Check Cyclic Permutations (Strings) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

[CHALLENGE] Check Cyclic Permutations (Strings)

Write a program that checks if two Strings are cyclic. Examples: String A: "1234" String B: "3412" The sequence B is equals to A, the only difference is it starts at another character. Another example with true result: String A: "100111011001" String B: "111011001100" All languages are welcome!

29th Jan 2018, 2:30 PM
Lucas Sousa
Lucas Sousa - avatar
9 Answers
29th Jan 2018, 6:25 PM
LukArToDo
LukArToDo - avatar
+ 16
https://code.sololearn.com/cz1NJK8e8OO1/?ref=app
29th Jan 2018, 4:22 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 6
@louis I'm still not sure what cyclic permutation is.. Is it the numbers formed by shuffling the initial number. Like is 15690 and 06951 cyclic?
29th Jan 2018, 5:43 PM
Yash✳️
Yash✳️ - avatar
+ 5
@Louis The input() returns a string.. So you're not converting it to integer. And hence you can't use sorted
29th Jan 2018, 4:38 PM
Yash✳️
Yash✳️ - avatar
+ 3
input is supposed to contain numbers only, right???
29th Jan 2018, 2:50 PM
‎ ‏‏‎Anonymous Guy
+ 3
my code may not be efficient but i guess it works properly and please report bugs https://code.sololearn.com/cVVgK41uydqM/?ref=app
29th Jan 2018, 5:14 PM
‎ ‏‏‎Anonymous Guy
+ 3
here's the corrected version of my code https://code.sololearn.com/cJaDSgT8HA3w/?ref=app
30th Jan 2018, 12:58 PM
‎ ‏‏‎Anonymous Guy
+ 2
@Sreejith Feel free with input content.
29th Jan 2018, 3:17 PM
Lucas Sousa
Lucas Sousa - avatar
+ 1
@yash 15690 and 06951 aren't cyclic. Look, to be cyclic the sequence needs to be "equals". Take another example, sequence a is "hello", sequence b is "ohell". True cyclic again. You can view this as a "rotated" sequence. If you look to a clock you will find the sequence: "1 2 3 4 5 6 7 8 9 10 11 12" The number 12 is at clock's top. But if you rotate the entire clock clockwise and put the, for example, number 7 on top, the sequence stills the the same, but starting at 7. Look: "7 8 9 10 11 12 1 2 3 4 5 6" Then, we conclude that: "1 2 3 4 5 6 7 8 9 10 11 12" and "7 8 9 10 11 12 1 2 3 4 5 6" are cyclic. My approach to check this is just as @LukArToDo suggested. Let's see: String A = "abcd"; String B ="dabc"; String aa = A+A; //"abcdabcd" Result: aa.contains(B); --------------------- If you look to aa.charAt(3) you'll find exactly the B sequence. Then this logic solves the challenge/problem.
29th Jan 2018, 9:22 PM
Lucas Sousa
Lucas Sousa - avatar