1768. Merge Strings Alternately

class Solution {

    /**
     * @param String $word1
     * @param String $word2
     * @return String
     */
    function mergeAlternately($word1, $word2) {
        $merged = '';
        $index = 0;
        while($word1 !== '' || $word2 !== '') {
            if (isset($word1[$index])) {
                $merged .= $word1[$index];
            } else {
                $word1 = '';
            }
            if (isset($word2[$index])) {
                $merged .= $word2[$index];
            } else {
                $word2 = '';
            }

            $index++;
        }

        return $merged;
    }
}

Last updated