diff --git a/leetcode/58.LengthofLastWord/lengthOfLastWord.go b/leetcode/58.LengthofLastWord/lengthOfLastWord.go new file mode 100644 index 0000000..7c8cea3 --- /dev/null +++ b/leetcode/58.LengthofLastWord/lengthOfLastWord.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" +) + +func lengthOfLastWord(s string) int { + i := len(s) - 1 + for i >= 0 && s[i] == ' ' { + i-- + } + + j := i + for j >= 0 && s[j] != ' ' { + j-- + } + + return i - j +} + +func main() { + s := "luffy is still joyboy" + fmt.Println(lengthOfLastWord(s)) +}