[ LeetCode] 58 Length of Last Word

codesver·2023년 7월 4일
0

LeetCode

목록 보기
14/24
post-thumbnail

📌 Problem

문자열은 영문자와 공백으로만 이루어져 있다. 공백을 제외한 마지막 단어의 길이를 반환하면 된다.

📌 Solution

문자열의 마지막 공백 문자열을 제거하고 마지막 문자열의 길이를 반환하면 된다.

📌 Code

fun lengthOfLastWord(s: String): Int =
    s.reversed().replaceFirst(Regex(" *"), "")
        .mapIndexed { idx, ch -> if (ch == ' ') return idx }.size
profile
Hello, Devs!

0개의 댓글