Length of Last Word | Leetcode Top Interview 150

Поділитися
Вставка
  • Опубліковано 18 вер 2024
  • Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies.
    About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer.
    Problem Link - leetcode.com/p...
    Problem Description - Given a string s consisting of words and spaces, return the length of the last word in the string.
    A word is a maximal
    substring
    consisting of non-space characters only.

КОМЕНТАРІ • 1

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086  Місяць тому +1

    Solution code - class Solution {
    public:
    int lengthOfLastWord(string s) {
    int n = s.length();
    int count = 0;
    int i;
    for (i=n-1;i>=0;i--) {
    if (s[i] != ' ')
    break;
    }
    for (;i>=0;i--) {
    if (s[i] != ' ') {
    count++;
    } else {
    break;
    }
    }
    return count;
    }
    };