From d350f1aceaa634626eeca3658ba797d0a3553447 Mon Sep 17 00:00:00 2001 From: rrbharath Date: Tue, 28 Jul 2026 17:13:56 -0400 Subject: [PATCH] Add Dart solution for Length of Last Word --- dart/length_of_last_word.dart | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 dart/length_of_last_word.dart diff --git a/dart/length_of_last_word.dart b/dart/length_of_last_word.dart new file mode 100644 index 0000000..e7d0869 --- /dev/null +++ b/dart/length_of_last_word.dart @@ -0,0 +1,23 @@ +/* +Given a string s consisting of words and spaces, return the length of the last word in the string. + +A word is a maximal substring consisting of non-space characters only. +*/ + +class Solution { + int lengthOfLastWord(String s) { + int index = s.length - 1; + int length = 0; + + while (index >= 0 && s[index] == ' ') { + index--; + } + + while (index >= 0 && s[index] != ' ') { + length++; + index--; + } + + return length; + } +}