From 2ada952bd91d60885908441a1a5906dc07698f06 Mon Sep 17 00:00:00 2001 From: x33r0 Date: Sat, 1 Oct 2022 23:50:13 +0200 Subject: [PATCH] [ADD] Add ts solution for problem (58. Length of Last Word) --- typescript/58-Length-of-Last-Word.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 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..9ccd0c2a3 --- /dev/null +++ b/typescript/58-Length-of-Last-Word.ts @@ -0,0 +1,24 @@ +// ============== [Sol1] =================== +function lengthOfLastWord(s: string): number { + let count = 0; + let startCount = false; + + for(let i = s.length -1 ; i >= 0; i--) { + if(s[i].match(/[a-z]/i) && !startCount) startCount = true; + else if(!s[i].match(/[a-z]/i) && startCount) return count; + if(s[i].match(/[a-z]/i)) count++; + } + + return count; +}; + + + +// ============== [Sol2] =================== +/* +function lengthOfLastWord(s: string): number { + + let res = s.trim().split(" ").filter(e => e.length != 0); + + return res[res.length -1].length; +};