Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 11, 2020 02:49 pm GMT

Daily Challenge 268 - Swapping Characters in Strings

You are given two strings s and t. Both strings have length n and consist of lowercase letters.

You can swap any two adjacent characters of s any number of times.
Output the minimum number of moves to transform s to t. If it is impossible to obtain the string t using moves, return -1.

Examples

('abcdef', 'abdfec') => 4
('abcd', 'accd') => -1
('ab', 'ab') => 0
('ab', 'ba') => 1
('aaa', 'aaa') => 0

Tests

{s:'abcdef', t:'abdfec'}
{s:'abcd', t:'accd'}

{s:'ab', t:'ab'}
{s:'ab', t:'ba'}
{s:'aaa', t:'aaa'}

Good luck!

This challenge comes from arhigod on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email [email protected] with your suggestions!


Original Link: https://dev.to/thepracticaldev/daily-challenge-268-swapping-characters-in-strings-4n1g

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To