1768. Merge Strings Alternately
  • 구현1
class Solution {
    public String mergeAlternately(String word1, String word2) {
        int length1 = word1.length();
        int length2 = word2.length();
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < Math.max(length1, length2); i++){

            if (i < length1) sb.append(word1.charAt(i));

            if (i < length2) sb.append(word2.charAt(i));

        }

        return sb.toString();

    }

}
  • 구현2
class Solution {
    public String mergeAlternately(String word1, String word2) {
        int m = word1.length();
        int n = word2.length();
        StringBuilder result = new StringBuilder();
        int i = 0, j = 0;

        while (i < m || j < n) {
            if (i < m) {
                result.append(word1.charAt(i++));
            }
            if (j < n) {
                result.append(word2.charAt(j++));
            }
        }

        return result.toString();
    }
}
1071. Greatest Common Divisor of Strings
1431. Kids With the Greatest Number of Candies
605. Can Place Flowers
345. Reverse Vowels of a String
151. Reverse Words in a String
238. Product of Array Except Self
334. Increasing Triplet Subsequence
443. String Compression