Write a function that returns the minimum number of swaps to convert the first binary string into the second.
Examples
minSwaps("1100", "1001") ➞ 1 minSwaps("110011", "010111") ➞ 1 minSwaps("10011001", "01100110") ➞ 4
Notes
- Both binary strings will be of equal length.
- Both binary strings will have an equal number of zeroes and ones.
- A swap is switching two elements in a string (swaps do not have to be adjacent).
Solution:
1 2 3 4 5 6 7 8 9 | function minSwaps(s1, s2) { let sol = 0; for(let i=0;i<s1.length;i++ ){ if(s1[i] !== s2[i]){ sol++; } } return sol/2; } |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.