Tensorum
YouTubeশেখা শুরু করুন
LeetCode 58 · Other

Length of Last Word

LeetCode 58 · সহজ · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

Skip trailing spaces, count back to a space.

সহজString
LeetCode-এ খুলুন ↗

আমি Length of Last Word (LeetCode 58) এভাবে সমাধান করেছি — Other প্যাটার্নের একটি সহজ প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।

নিচের বিবরণ ইংরেজিতে।

Intuition

Scan from the end: skip trailing spaces, then count characters until the next space or the start.

Approach

  1. 1

    i at the last index; move left past spaces.

  2. 2

    Count non-space characters moving left.

আমি কীভাবে সমাধান করেছি

Reverse scanPythonO(n) time
class Solution:
    def lengthOfLastWord(self, s: str) -> int:
        #trime white space from right
        i=len(s)-1
        while i>=0 and s[i]==" ":
            i-=1
        length=0
        while i>=0 and s[i]!=" ":
            length+=1
            i-=1
        return length
স্টেপ-থ্রু লোড হচ্ছে…

Length of Last Word — সাধারণ প্রশ্ন

Length of Last Word-এর পেছনে intuition কী?

Scan from the end: skip trailing spaces, then count characters until the next space or the start.

Length of Last Word (LeetCode 58) কীভাবে সমাধান করবেন?

1. i at the last index; move left past spaces. 2. Count non-space characters moving left.

Length of Last Word-এর time আর space complexity কত?

Time: O(n)। Space: O(1)। (Accepted approach: Reverse scan।)

Length of Last Word-এ সাধারণ ভুলগুলো কী?

Trailing spaces are the trap.