-
Notifications
You must be signed in to change notification settings - Fork 0
/
LengthOfLastWord.kt
136 lines (113 loc) · 2.61 KB
/
LengthOfLastWord.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Length of Last Word
Given a string s consisting of words and spaces, return the length of the last word in the string.
A word is a maximal substring consisting of non-space characters only.
Example 1:
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.
Example 2:
Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is "moon" with length 4.
Example 3:
Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.
Constraints:
1 <= s.length <= 104
s consists of only English letters and spaces ' '.
There will be at least one word in s.
*/
fun main() {
println(lengthOfLastWord("Hello World"))
println(lengthOfLastWord(" fly me to the moon "))
}
fun lengthOfLastWord(s: String): Int {
return s.trim().run { length - lastIndexOf(" ") - 1 }
}
fun lengthOfLastWord(s: String): Int {
val startIndex = s.trim().lastIndexOf(" ") - 1
return s.trim().length - startIndex
}
class Solution {
fun lengthOfLastWord(s: String): Int {
var count = 0
for (i in s.length - 1 downTo 0)
if (s[i] != ' ')
count++
else if (count > 0)
return count
return count
}
}
class Solution {
fun lengthOfLastWord(s: String): Int {
var count = 0
var start = false
for (i in s.length - 1 downTo 0) {
if (s[i] == ' ' && start) {
return count
}
if (start) {
count++
} else {
if (s[i] != ' ' && !start) {
start = true
count++
}
}
}
return count
}
}
class Solution {
fun lengthOfLastWord(message: String): Int {
val cleanMessage = message.trim()
val messages = cleanMessage.split(" ").reversed()
val lastWord = messages[0]
return lastWord.length
}
}
class Solution {
fun lengthOfLastWord(s: String): Int {
if (s.isEmpty()) return 0
var rightWordIndex = s.length - 1
// skip past trailing spaces
while (rightWordIndex >= 0 && s[rightWordIndex] == ' ') {
rightWordIndex--
}
var wordLen = 0
while (rightWordIndex >= 0 && s[rightWordIndex] != ' ') {
wordLen++
rightWordIndex--
}
return wordLen
}
}
class Solution {
fun lengthOfLastWord(s: String): Int {
val arr = s.trim().split(" ")
return arr[arr.size - 1].length
}
}
class Solution {
fun lengthOfLastWord(s: String): Int {
val stringArray = s.trim().split(" ")
val finalWord = stringArray[stringArray.size - 1]
return finalWord.length
}
}
class Solution {
fun lengthOfLastWord(s: String): Int {
val wordsArray = s.split(" ")
var j = wordsArray.size - 1
var maxCount = 0
while (maxCount <= 0) {
if (wordsArray[j] != " ")
maxCount = wordsArray[j].length
j--
}
return maxCount
}
}