Posts

Showing posts from May, 2023

Leetcode 75: 1768. Merge Strings Alternately

In this blog post, we'll examine a problem from Leetcode - "Merge Strings Alternately". We'll walk through a PHP solution and break it down for better understanding. The problem asks to merge two strings by adding letters in alternating order, starting with the first string. If one string is longer than the other, the additional letters should be appended to the end of the merged string. The PHP solution given below does exactly that: class Solution {     /**      * @param String $word1      * @param String $word2      * @return String      */     function mergeAlternately($word1, $word2) {         $word1Length = strlen($word1);         $word2Length = strlen($word2);         $output = '';         $remainder = '';         $loopLength = min($word1Length, $word2Length);         if ($word1Length > $word2Length) {             $remainder = substr($word1, $word2Length);         } else if ($word1Length < $word2Length) {             $remainder = substr($w