Swift Solution in 22ms


  • 0
    U
    class Solution {
        func lengthOfLastWord(_ s: String) -> Int {
    
            var length = 0
            
            let stringArray = s.components(separatedBy:" ")
            
            let filterArray = stringArray.filter { $0 != "" }
            
            if filterArray.count != 0 {
                length = (filterArray.last?.characters.count)!
            }
            
            return length
        }
    }
    
    

Log in to reply
 

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