[Kotlin] Korean Coding Test - Programmers - Creating a JadenCase String
KotlinQuestion
JadenCase is a string in which the first letter of any word is uppercase and the other alphabets are lowercase. However, if the first character is not an alphabet, the subsequent alphabet can be written in lowercase. (See First Input/Output Example)
Given a string s, complete a function, solution, that returns a string that replaces s with JadenCase.
Restrictions
s is a string with a length of 1 or more and 200 or less.
The s consists of an alphabet, a number, and a blank character ("").
- Numbers are only the first letter of a word.
- There is no word that only numbers.
- Space characters can occur consecutively.
Input/Output example
Input/Output example #1
- Input: "3people unFollowed me"
- Output: "3people Unfollowed Me"
Input/Output example #2
- Input: "for the last week"
- Output: "For The Last Week"
Code 1
class Solution {
fun solution(s: String): String {
var answer = ""
for (i in s.indices) {
if (i == 0)
answer = s[i].uppercase().toString()
else if (s[i] == ' ')
answer+= " "
else if (s[i - 1] == ' ')
answer+= s[i].uppercase().toString()
else
answer+= s[i].lowercase().toString()
}
return answer
}
}
TEST 1 〉 Pass (3.85ms, 60MB)
TEST 2 〉 Pass (3.63ms, 60.4MB)
TEST 3 〉 Pass (4.06ms, 61.6MB)
TEST 4 〉 Pass (4.82ms, 60.8MB)
TEST 5 〉 Pass (3.77ms, 61.8MB)
TEST 6 〉 Pass (3.52ms, 61.6MB)
TEST 7 〉 Pass (4.02ms, 58.9MB)
TEST 8 〉 Pass (4.83ms, 58.3MB)
TEST 9 〉 Pass (4.19ms, 59.3MB)
TEST 10 〉 Pass (2.43ms, 62.2MB)
TEST 11 〉 Pass (5.22ms, 59.7MB)
TEST 12 〉 Pass (3.74ms, 60.6MB)
TEST 13 〉 Pass (5.78ms, 60.1MB)
TEST 14 〉 Pass (5.53ms, 59.2MB)
TEST 15 〉 Pass (5.28ms, 60MB)
TEST 16 〉 Pass (7.31ms, 60.4MB)
TEST 17 〉 Pass (3.45ms, 61.7MB)
TEST 18 〉 Pass (0.04ms, 61.8MB)
Code 2
class Solution {
fun solution(s: String): String {
var answer = ""
for (i in s.indices) {
if (i == 0 || s[i] == ' ' || s[i-1] == ' ')
answer+= s[i].uppercase().toString()
else
answer+= s[i].lowercase().toString()
}
return answer
}
}
TEST 1 〉 Pass (3.78ms, 61.2MB)
TEST 2 〉 Pass (2.79ms, 59.4MB)
TEST 3 〉 Pass (4.42ms, 62.8MB)
TEST 4 〉 Pass (3.89ms, 60MB)
TEST 5 〉 Pass (3.21ms, 61.1MB)
TEST 6 〉 Pass (4.04ms, 61.3MB)
TEST 7 〉 Pass (3.00ms, 60.9MB)
TEST 8 〉 Pass (4.13ms, 62.7MB)
TEST 9 〉 Pass (2.52ms, 61.7MB)
TEST 10 〉 Pass (2.46ms, 59.7MB)
TEST 11 〉 Pass (3.69ms, 62.4MB)
TEST 12 〉 Pass (2.98ms, 60.9MB)
TEST 13 〉 Pass (2.72ms, 60.4MB)
TEST 14 〉 Pass (2.38ms, 62MB)
TEST 15 〉 Pass (3.79ms, 60.8MB)
TEST 16 〉 Pass (2.69ms, 61.4MB)
TEST 17 〉 Pass (2.70ms, 62.6MB)
TEST 18 〉 Pass (3.48ms, 60.7MB)
Code 3. Use replaceFirstChar function
class Solution {
fun solution(s: String): String {
return s.lowercase().split(" ").joinToString(" ") { it.replaceFirstChar { str -> str.uppercase() } }
}
}
TEST 1 〉 Pass (14.24ms, 61.9MB)
TEST 2 〉 Pass (22.73ms, 61.6MB)
TEST 3 〉 Pass (25.42ms, 61.2MB)
TEST 4 〉 Pass (18.18ms, 60.2MB)
TEST 5 〉 Pass (19.63ms, 62.3MB)
TEST 6 〉 Pass (18.36ms, 61.6MB)
TEST 7 〉 Pass (16.31ms, 62MB)
TEST 8 〉 Pass (15.76ms, 61.6MB)
TEST 9 〉 Pass (17.78ms, 61.1MB)
TEST 10 〉 Pass (18.27ms, 62MB)
TEST 11 〉 Pass (13.90ms, 60.9MB)
TEST 12 〉 Pass (15.15ms, 61.7MB)
TEST 13 〉 Pass (15.64ms, 61.2MB)
TEST 14 〉 Pass (18.15ms, 62MB)
TEST 15 〉 Pass (19.55ms, 61.1MB)
TEST 16 〉 Pass (17.54ms, 61.6MB)
TEST 17 〉 Pass (15.81ms, 61.2MB)
TEST 18 〉 Pass (12.84ms, 61.3MB)