DEV Community

King coder
King coder

Posted on • Edited on

DSA Problem Solving โ€” Day 3

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

Enter fullscreen mode Exit fullscreen mode

โš™๏ธ Approach 1: Using trim() and split(' ')

๐Ÿ” Step-by-Step:

  1. Use trim() (or rstrip() in Python) to remove extra spaces from the beginning and end of the string.
  2. Use split(' ') to convert the string into an array of words.
  3. 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;
};

Enter fullscreen mode Exit fullscreen mode

โœ… 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 in haystack.
  • Return -1 if needle is not found.

๐Ÿงพ Examples

Input: haystack = "sadbutsad", needle = "sad"
Output: 0


Input: haystack = "leetcode", needle = "leeto"
Output: -1


Input: haystack = "aaaaa", needle = "bba"
Output:


Enter fullscreen mode Exit fullscreen mode

โš™๏ธ Approach: Manual Substring Comparison using slice()

๐Ÿ” Step-by-Step (Simple Explanation)

  1. First, get the length of the needle and store it in n.

  2. If n is 0, return 0 immediately. (This means we're looking for an empty string, which is always found at the beginning.)

  3. Use slice() to take a part of haystack that is the same length as needle.

  4. 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).
  5. 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
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)