Adobe Interview Question
Computer ScientistsCountry: India
Interview Type: In-Person
Just as you said, it is " similar to minimum swaps to convert string A to string B." so would we be given the string B in this case, which is "we must go to newyork now"?
1. If yes, then the decisive solution is same as conversion of string A to string B which takes some help from the Longest Common Sub-sequence dynamic prog problem.
2. If no, then I don't see if there is any decisive solution..
we can use inversion count approach
step 1: assign a number t each word of string A
and step 2: apply inversion count
geeksforgeeks archive : archives/3968
good solution bhavesh
@TechLead
We can assign a number to each word in the correct sentence
e.g.:
we must go to newyork now
we - 1
must - 2
go - 3
to - 4
newyork - 5
now - 6
do this using a map
now for the senetence spoken by yoda
"Newyork must go we to now"
create an int array storing numbers for the corresponding words
array would be {5,2,3,1,4,6}
count the number of inversions as done here: www[dot]geeksforgeeks[dot]org/archives/3968
Though this is nice approach, finding inversion wont give you minimum swaps required.but we can get minimum swaps form this new array.
- sajaljain4 August 30, 2012