Letter Problem (with.Java)
This article looks into the “letter” problem.
As I solve coding test problems, I look back on the problems I solved and look into different solution methods to get to know myself.
Let’s look at the problem first.
problem
I’m going to write a happy birthday letter to my shy grandmother.
I am trying to write each letter at a size of 2cm horizontally so that my grandmother can easily see it. When writing the letter only horizontally, please complete the solution function to return the minimum horizontal length of the letter paper needed to write the congratulatory message.
Restrictions
- Spaces are also treated as one character.
- 1 ≤ message length ≤ 50
- I don’t think about the blank space on the letterhead.
- The message consists only of upper and lower case letters of the English alphabet, ‘!’, ‘~’, or spaces.
Input/Output Example
message | result |
---|---|
“Happy birthday!” | 30 |
“I love you~” | 22 |
My solution to the problem
class Solution {
public int solution(String message) {
int answer = message.length() * 2;
return answer;
}
}
Solution explanation
- This method accepts a message parameter of String type, stores the length of the message multiplied by 2 in the answer variable, and returns an answer.
- This code returns a value that doubles the length of the given string.