Rust Merge Strings Alternately
Table of Contents
This posts comes from the real project scenario. Later, I surprisedly find there is one problem in Leetcode. Personally I not a fan of solving Leetcode problems. Let’s write a post here since they provide detailed descriptions.
The problem is easy-level one and not complicated. So, just make it become onf of my cheatseets then.
Description
Source:Click Me: Leetcode-1768
Description:
You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.
Return the merged string.
Example 1:
Example 2:
Example 3:
Solution
The algorithm itself is quite simple so I won’t repeat this in here. Let’s show the code directly:
Updates
20210520 Updates:
Later, I suddenly find a crate named itertools, which is quite convenient for us to release heavy lifting work.
For example, we are able to merge, ordering-style merge(kmerge), create an iterator over the “cartesian product” of iterators, as well as interleaving.
No more talking! Let’s check the code below:
use Itertools;
// I know I can simplify code below but I insist this.