-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLengthOfLastWord.cs
40 lines (37 loc) · 1.35 KB
/
LengthOfLastWord.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LeetCodeSolutionsLib
{
/// <summary>
/// 58. Length of Last Word
/// Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.
/// IE) Input : "Hello World"
/// Output : 5
/// </summary>
public class LengthOfLastWord : Solution
{
private string _inputString;
public LengthOfLastWord(string inputString)
{
this._inputString = inputString;
}
private int lengthOfLastWord(string inputString)
{
// Special Case: Trim White Spaces at the end of the word
// Separate string by space, Get Last Word's Length
return inputString.TrimEnd().Split(' ').Last().Length;
}
public override void PrintExample()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
var results = lengthOfLastWord(this._inputString);
watch.Stop();
Console.WriteLine($"58. Length of Last Word\n" +
$"Input String = {this._inputString} \n" +
$"Result: [{results}] \n" +
$"Execution Speed: {watch.ElapsedMilliseconds}ms \n");
}
}
}