Tuesday, March 31, 2015

LeetCode OJ - Length of Last Word

Problem:

Please find the problem here.

Solution:

Standard problem on scanning and state machine for it, just that it goes from backward, so be it.

Code:


#include "stdafx.h"

// https://leetcode.com/problems/length-of-last-word/

#include "LEET_LENGTH_OF_LAST_WORD.h"
#include <map>
#include <iostream>
#include <vector>

using namespace std;

namespace _LEET_LENGTH_OF_LAST_WORD
{
    class Solution
    {
    public:
        int lengthOfLastWord(const char *s)
        {
            int length = strlen(s);
            int state = 0;
            int result = 0;
            for (int i = length - 1; i >= 0; i--)
            {
                if (state == 0)
                {
                    if (s[i] != ' ')
                    {
                        result = 1;
                        state = 1;
                    }
                }
                else if (state == 1)
                {
                    if (s[i] != ' ')
                    {
                        result++;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return result;
        }
    };
};

using namespace _LEET_LENGTH_OF_LAST_WORD;

int LEET_LENGTH_OF_LAST_WORD()
{
    Solution solution;
    cout << solution.lengthOfLastWord("Hello World") << endl;
    cout << solution.lengthOfLastWord("Hello World ") << endl;
    cout << solution.lengthOfLastWord(" ") << endl;
    cout << solution.lengthOfLastWord("Hello") << endl;
    cout << solution.lengthOfLastWord("Hello ") << endl;
    return 0;
}

No comments :

Post a Comment