+ 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!
9 Antworten
+ 16
https://code.sololearn.com/cz1NJK8e8OO1/?ref=app
+ 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?
+ 5
@Louis The input() returns a string.. So you're not converting it to integer. And hence you can't use sorted
+ 3
input is supposed to contain numbers only, right???
+ 3
my code may not be efficient but i guess it works properly and please report bugs
https://code.sololearn.com/cVVgK41uydqM/?ref=app
+ 3
here's the corrected version of my code
https://code.sololearn.com/cJaDSgT8HA3w/?ref=app
+ 2
@Sreejith Feel free with input content.
+ 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.