1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00
LeetCode/cs/length-of-last-word.cs
Matej Focko 49defb257e
cs: add «58. Length of Last Word»
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-04-01 21:30:57 +02:00

25 lines
495 B
C#

public class Solution {
public int LengthOfLastWord(string s) {
var lastLength = 0;
var length = 0;
foreach (var c in s) {
if (char.IsWhiteSpace(c)) {
if (length != 0) {
lastLength = length;
}
length = 0;
continue;
}
++length;
}
if (length != 0) {
lastLength = length;
}
return lastLength;
}
}