Q1 โ Length of Last Word
๐ Difficulty: Easy
๐ Problem Link: Length of Last Word - LeetCode
My Solution: ๐ Solution Link: Length of Last Word - LeetCode
๐ง Problem Description
You're given a string s
that contains words and spaces. Your goal is to return the length of the last word in the string.
- A word is a maximal substring made up of non-space characters only.
- The string may contain leading, trailing, or multiple spaces between words.
Example
Input: "Hello World"
Output: 5
Input: " fly me to the moon "
Output: 4
Input: "luffy is still joyboy"
Output: 6
โ๏ธ Approach 1: Using trim()
and split(' ')
๐ Step-by-Step:
- Use
trim()
(orrstrip()
in Python) to remove extra spaces from the beginning and end of the string. - Use
split(' ')
to convert the string into an array of words. - Return the length of the last element in that array.
var lengthOfLastWord = function(s) {
// Step 1: Trim leading and trailing spaces
let trimmed = s.trim();
// Step 2: Split the string by spaces
let words = trimmed.split(' ');
// Step 3: Return the length of the last word
return words[words.length - 1].length;
};
โ LeetCode โ Implement strStr()
๐ Difficulty: Easy
๐ Problem Link: Implement strStr() - LeetCode
My Solution: ๐ Solution Link: Find the Index of the First Occurrence in a String
๐ง Problem Description
Given two strings, haystack
and needle
, return the index of the first occurrence of needle
in haystack
. If needle
is not part of haystack
, return -1
.
๐ฅ Input
-
haystack
: The string where we search (e.g.,"sadbutsad"
) -
needle
: The substring we want to find (e.g.,"sad"
)
๐ค Output
- An integer representing the starting index of the first occurrence of
needle
inhaystack
. - Return
-1
ifneedle
is not found.
๐งพ Examples
Input: haystack = "sadbutsad", needle = "sad"
Output: 0
Input: haystack = "leetcode", needle = "leeto"
Output: -1
Input: haystack = "aaaaa", needle = "bba"
Output:
โ๏ธ Approach: Manual Substring Comparison using slice()
๐ Step-by-Step (Simple Explanation)
First, get the length of the
needle
and store it inn
.If
n
is0
, return0
immediately. (This means we're looking for an empty string, which is always found at the beginning.)Use
slice()
to take a part ofhaystack
that is the same length asneedle
.-
Loop through
haystack
and at each step:- Compare the current slice to
needle
. - If it matches, return the current index.
- If not, take the next slice (move forward by 1 character).
- Compare the current slice to
If no match is found after checking all valid positions, return
-1
.
โ Sample Code (JavaScript)
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function(haystack, needle) {
let n = needle.length;
// If needle is an empty string, return 0
if (n === 0) return 0;
// Get the first slice from haystack to compare
let str1 = haystack.slice(0, n);
let str2 = needle;
// Loop through haystack
for (let i = 0; str1.length === n && i < haystack.length; i++) {
if (str1 === str2) {
return i; // Found a match
}
// Move the window one step forward
str1 = haystack.slice(i + 1, i + 1 + n);
}
return -1; // No match found
};
Top comments (0)