-
Notifications
You must be signed in to change notification settings - Fork 41
/
58.swift
57 lines (45 loc) · 1.36 KB
/
58.swift
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
//
// LengthOfLastWord.swift
// LeetCode
//
// Created by Lex Tang on 5/5/15.
// Copyright (c) 2015 Lex Tang. All rights reserved.
//
/*
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
For example,
Given s = "Hello World",
return 5.
*/
import Foundation
import XCTest
extension String {
func lengthOfLastWord() -> Int {
var wordLength = 0
var cnt = 0
let spaceChar: Character = " "
for (_, c) in "\(self) ".enumerated() {
if c != spaceChar {
cnt += 1
} else {
if cnt > 0 {
wordLength = cnt
}
cnt = 0
}
}
return wordLength
}
}
class LengthOfLastWordTest: XCTestCase {
func testLengthOfLastWord() {
XCTAssertEqual("Hello world".lengthOfLastWord(), 5, "")
XCTAssertEqual("Helloworld".lengthOfLastWord(), 10, "")
XCTAssertEqual("a".lengthOfLastWord(), 1, "")
XCTAssertEqual(" ".lengthOfLastWord(), 0, "")
XCTAssertEqual("Hello world ".lengthOfLastWord(), 5, "")
XCTAssertEqual("abc defg hijkl".lengthOfLastWord(), 5, "")
}
}