blog.itcode.devblog.itcode.dev

[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".

[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".
RWB0104
@RWBwritten at 2021-12-18 08:15:21
Programmers

시리즈 모아보기

Programmers

46 / 78
RankLanguage 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.
nreturn
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
ship
blog.itcode.dev

Notes from the π-th Alpaca

7.0.1
Developed by RWB since 2021.057th upgraded at 2026.08