-
1768. Merge Strings Alternately javaLeetCode_Study_Plan/Programming Skills 2022. 9. 21. 14:07
https://leetcode.com/problems/merge-strings-alternately/?envType=study-plan&id=programming-skills-i
class Solution { public String mergeAlternately(String word1, String word2) { String[] word1Arr = word1.split(""); String[] word2Arr = word2.split(""); ArrayList<String> ansList = new ArrayList<String>(); if(word2Arr.length - word1Arr.length > 0){ for (int i = 0; i < word1Arr.length; i++){ ansList.add(word1Arr[i]); ansList.add(word2Arr[i]); } for (int j=word1Arr.length; j < word2Arr.length; j++){ ansList.add(word2Arr[j]); } } else{ for (int i = 0; i < word2Arr.length; i++){ ansList.add(word1Arr[i]); ansList.add(word2Arr[i]); } for (int j=word2Arr.length; j < word1Arr.length; j++){ ansList.add(word1Arr[j]); } } String ans = ""; for(String item: ansList){ ans += item; } return ans; } }
뭔가 이 케이스 분류를 더 일반화해서 풀 수 있는 로직이 있을 것 같긴하다
'LeetCode_Study_Plan > Programming Skills' 카테고리의 다른 글
389. Find the Difference java (0) 2022.09.21 1678. Goal Parser Interpretation java (1) 2022.09.21 566. Reshape the Matrix java (1) 2022.09.21 1572. Matrix Diagonal Sum java (0) 2022.09.21 1672. Richest Customer Wealth java (0) 2022.09.21