Given a string s
consisting of words separated by spaces, you need to determine the length of the last word in the string. The string may contain leading and trailing spaces, and words are separated by one or more spaces.
Input: str = "hello aman"
Output: 4
function getLengthOfLastWord(s) {
if (typeof s !== 'string') throw new TypeError('Input must be a string');
s = s.trim();
const words = s.split(' ');
return words[words.length - 1].length;
}
const str1 = "hello aman";
getLengthOfLastWord(str1); //output: 4
function findLengthOfLastWordUsingRegex(s) {
if (typeof s !== 'string') throw new TypeError('Input must be a string');
const match = s.trim().match(/\S+$/);
return match ? match[0].length : 0;
}
const str1 = "hello aman";
findLengthOfLastWordUsingRegex(str1); //output: 4
function calculateLengthOfLastWord(s) {
if (typeof s !== 'string') throw new TypeError('Input must be a string');
let length = 0;
let i = s.length - 1;
while (i >= 0 && s[i] === ' ') {
i--;
}
while (i >= 0 && s[i] !== ' ') {
length++;
i--;
}
return length;
}
const str1 = "hello aman";
calculateLengthOfLastWord(str1); //output: 4
function lengthOfLastWordWithReduce(s) {
if (typeof s !== 'string') throw new TypeError('Input must be a string');
return s.trim().split(/\s+/).reduce((_, word) => word.length, 0);
}
const str1 = "hello aman";
lengthOfLastWordWithReduce(str1); //output: 4
function lengthOfLastWordUsingForEach(s) {
if (typeof s !== 'string') throw new TypeError('Input must be a string');
let lastWordLength = 0;
s.trim().split(/\s+/).forEach(word => lastWordLength = word.length);
return lastWordLength;
}
const str1 = "hello aman";
lengthOfLastWordUsingForEach(str1); //output: 4
function getLastWordLength(s) {
if (typeof s !== 'string') throw new TypeError('Input must be a string');
const words = s.trim().split(/\s+/);
return words.pop().length;
}
const str1 = "hello aman";
getLastWordLength(str1); //output: 4
function lengthOfLastWordWithLastIndexOf(s) {
if (typeof s !== 'string') throw new TypeError('Input must be a string');
s = s.trim();
const lastSpace = s.lastIndexOf(' ');
return s.length - lastSpace - 1;
}
const str1 = "hello aman";
lengthOfLastWordWithLastIndexOf(str1); //output: 4
function lengthOfLastWordUsingMap(s) {
if (typeof s !== 'string') throw new TypeError('Input must be a string');
const words = s.trim().split(/\s+/);
return words.map(word => word.length).pop();
}
const str1 = "hello aman";
lengthOfLastWordUsingMap(str1); //output: 4
function lengthOfLastWordUsingStack(s) {
if (typeof s !== 'string') throw new TypeError('Input must be a string');
const stack = [];
for (const char of s.trim()) {
if (char === ' ') {
stack.length = 0;
} else {
stack.push(char);
}
}
return stack.length;
}
const str1 = "hello aman";
lengthOfLastWordUsingStack(str1); //output: 4
function getLengthOfLastWordUsingSubstring(s) {
if (typeof s !== 'string') throw new TypeError('Input must be a string');
s = s.trim();
const lastSpaceIndex = s.lastIndexOf(' ');
return s.substring(lastSpaceIndex + 1).length;
}
const str1 = "hello aman";
getLengthOfLastWordUsingSubstring(str1); //output: 4