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
needleinhaystack. - Return
-1ifneedleis 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
needleand store it inn.If
nis0, return0immediately. (This means we're looking for an empty string, which is always found at the beginning.)Use
slice()to take a part ofhaystackthat is the same length asneedle.-
Loop through
haystackand 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)