[Programmers / JAVA] Level 1 Watermelon Watermelon Watermelon Watermel...? (12922)
[Programmers / JAVA] Level 1 Watermelon Watermelon Watermelon Watermel...? (12922)
Complete the function solution, which returns a string of length n that maintains a pattern like "watermelonwatermelonwatermelonwat....". For example, if n is 4, it should return "wate", and if n is 3, it should return "wat".
@RWBwritten at 2021-12-18 08:15:21
| Rank | Language Used |
|---|---|
| Level 1 | 🖼️ JAVA |
🔗 Watermelon Watermelon Watermelon Watermel...?
Complete the function solution, which returns a string of length n that maintains a pattern like "watermelonwatermelonwatermelonwat....". For example, if n is 4, it should return "wate", and if it is 3, it should return "wat".
Note: the original Korean problem is based on the repeating syllables "수" and "박" (from "수박", meaning watermelon).
- n is a natural number no greater than 10,000 in length.
| n | return |
|---|---|
| 3 | "수박수" |
| 4 | "수박수박" |
Depending on the string length n, we just need to build a string in the form 수박수박수박....
We repeat n times, appending 수 for even indices and 박 for odd indices, to build the string.
JAVA
/** * 수박수박수박수박수박수? class * * @author RWB * @since 2021.12.13 Mon 16:05:46 */ class Solution { /** * Method that returns the answer * * @param n: [int] natural number * * @return [String] answer */ public String solution(int n) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < n; i++) { builder.append(i % 2 == 0 ? "수" : "박"); } return builder.toString(); } }
# Programmers# Algorithm# JAVA# Level 1
