-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path58. Length of Last Word.js
58 lines (46 loc) · 1.29 KB
/
58. Length of Last Word.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
58. Length of Last Word
https://leetcode.com/problems/length-of-last-word/
https://leetcode.com/problems/length-of-last-word/discuss/2407083/easy-java-script-two-way-solution-with-ologn-and-on-time-complexity
*/
/*
Example 1:
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.
Example 2:
Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is "moon" with length 4.
Example 3:
Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.
*/
/**
* @param {string} s
* @return {number}
*/
/* METHOD1 WITH ⏱️ TIME COMPLEXITY O(LogN) */
var lengthOfLastWord = function (s) {
let count = 0;
for (let i = s.length - 1; i >= 0; i--) {
if (s[i] == " ") {
if (count == 0) {
continue;
} else {
return count;
}
}
if ((s[i].charCodeAt() >= 97 && s[i].charCodeAt() <= 122) || (s[i].charCodeAt() >= 65 && s[i].charCodeAt() <= 90)) {
count++;
}
}
return count;
};
/* METHOD2 WITH ⏱️ TIME COMPLEXITY O(N) */
var lengthOfLastWord = function (s) {
var n = s.trim().split(" "); // O(N);
return n[n.length - 1].length;
}
console.log(lengthOfLastWord("Hello WorldZ "));