Google interview question
Anonymous User
1824

Let S be a string containing only letters of the English alphabet. An anagram of S is any string that contains exactly the same letters as S (with the same number of occurrences for each letter), but in a different order. For example, the word kick has anagrams such as kcik and ckki.

Now, let S[i] be the i-th letter in S. We say that an anagram of S, A, is shuffled if and only if for all i, S[i]≠A[i]. So, for instance, kcik is not a shuffled anagram of kick as the first and fourth letters of both of them are the same. However, ckki would be considered a shuffled anagram of kick, as would ikkc.

Given an arbitrary string S, your task is to output any one shuffled anagram of S, or else print -1 if this cannot be done.

Example -
start -> trsta
jjj -> -1
Constraints -
1 <= S.length <= 10^4
Can anyone provide an optimal approach for this problem
#google #googleinterview #googlequestion

Comments (7)