Swift Solution - 19ms (95%)


  • 0
    Z
    class Solution {
        func lengthOfLastWord(_ s: String) -> Int {
            if s.isEmpty {
                return 0
            }
            
            return s.trimmingCharacters(in: .whitespacesAndNewlines).components(separatedBy: " ").last!.characters.count
        }
    }
    

    You can break up the return statement if you'd like for readability, but this is pretty simple and straightforward.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.