From a320856cc6b517e0de84730c5bfd2814ee8093f8 Mon Sep 17 00:00:00 2001 From: loczek <30776250+loczek@users.noreply.github.com> Date: Tue, 1 Nov 2022 20:24:59 +0100 Subject: [PATCH] Created: 58-Length-of-Last-Word.ts --- typescript/58-Length-of-Last-Word.ts | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 typescript/58-Length-of-Last-Word.ts diff --git a/typescript/58-Length-of-Last-Word.ts b/typescript/58-Length-of-Last-Word.ts new file mode 100644 index 000000000..c66e5959c --- /dev/null +++ b/typescript/58-Length-of-Last-Word.ts @@ -0,0 +1,9 @@ +function lengthOfLastWord(s: string): number { + let res = 0; + for (let i = s.length - 1; i > -1; i--) { + if (s[i] === ' ' && res === 0) continue; + if (s[i] === ' ') break; + res += 1; + } + return res; +}