Length of Last Word
LeetCode 58 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Skip trailing spaces, count back to a space.
நான் 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
i at the last index; move left past spaces.
- 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 lengthLength 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.