All Permutations of a string when corresponding characters are not in the same place

Posted by r20rock on Stack Overflow See other posts from Stack Overflow or by r20rock
Published on 2012-11-03T09:33:22Z Indexed on 2012/11/03 11:00 UTC
Read the original article Hit count: 272

Filed under:
|
|
|
|

I need all possible permutations of a given string such that no character should remain at the same place as in the input string. Eg. : for input "ask" Output: all possible permutaions like "ksa", "kas"... such that 'a' is not in the 1st position , 's' is not in the 2nd positions and so on... in any permutation.

I only need the count of such possible permutations

I can do this by generating all permutations and filtering them but I need a very efficient way of doing this.

All characters in the string are "UNIQUE"

Preferred language C++.

© Stack Overflow or respective owner

Related posts about c++

Related posts about c