Interview Classic 150 Questions P58 Length of Last Word even6292025-10-12 (Updated: 2025-10-12) algorithmLeetCode Interview 150, String, algorithm Timeline timeline2025-10-12init string Problem:P58 Length of Last Wordhttps://leetcode.cn/problems/length-of-last-word/description/?envType=study-plan-v2&envId=top-interview-150Directly find two positions: the first is the end of the last word, the second is the position before the start of the last word. Note that start should be initialized to -1 to handle the case s = ‘a’.123456789101112131415161718192021222324252627282930#include <string>using std::string;class Solution {public: int lengthOfLastWord(string s) { int len = s.size(); int i = len - 1; int end = 0, start = -1; while (i >= 0) { if (std::isalpha(s[i])) { end = i; break; } i--; } while (i >= 0) { if (!std::isalpha(s[i])) { start = i; break; } i--; } return end - start; }};