You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+1
Original file line number
Diff line number
Diff line change
@@ -71,6 +71,7 @@ List of Programs related to data structures and algorithms
71
71
| 9 | Encode and decode strings |[Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/9.encodeDecodeStrings/encodeDecodeStrings.js)|[JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/9.encodeDecodeStrings/encodeDecodeStrings.js)|[Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/9.encodeDecodeStrings/encodeDecodeStrings.md)| Medium | Basic string and array operations |
72
72
| 10 | Greatest common devisor of strings |[Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/10.greatestCommonDivisor/greatestCommonDivisor.js)|[JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/10.greatestCommonDivisor/greatestCommonDivisor.js)|[Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/10.greatestCommonDivisor/greatestCommonDivisor.md)| Easy | Euclidean and String operations |
73
73
| 11 | Reverse words in string |[Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/11.reverseWordsInString/reverseWordsInString.js)|[JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/11.reverseWordsInString/reverseWordsInString.js)|[Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/11.reverseWordsInString/reverseWordsInString.md)| Medium | Basic string and array operations |
74
+
| 12 | Length of last word |[Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/12.lastWordLength/lastWordLength.js)|[JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/12.lastWordLength/lastWordLength.js)|[Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/12.lastWordLength/lastWordLength.md)| Easy | String traversal |
Given an input string `str` which consists of words and spaces, Return the length of last word in the string.
3
+
4
+
**Note:** The word is considered as a maximal substring with non-space characters.
5
+
6
+
### Examples
7
+
Example 1:
8
+
Input: "Welcome to DSA"
9
+
Output: 3
10
+
11
+
Example 2:
12
+
Input: " My pet is fluffy "
13
+
Output: 6
14
+
15
+
**Algorithmic Steps**
16
+
This problem is solved with the help of string traversal. The algorithmic approach can be summarized as follows:
17
+
18
+
1. Define the last index(i.e,`i`) of the given string and initialize length of last word(i.e, `length`) to 0.
19
+
20
+
2. Trim the ending spaces to find the last non-space character's index. This can be done through iteration from the end of a string.
21
+
22
+
3. Find the length of last word by iterating the string until the space character is found.
23
+
24
+
4. Return length of last word as an output.
25
+
26
+
**Time and Space complexity:**
27
+
This algorithm has a time complexity of `O(n)` where `n` is the length of given string. This is because we may need to iterate entire string incase of single word string(worst case scenario).
28
+
29
+
Also, it takes constant( `O(1)`) space complexity because of no additional variables required other than length output variable.
Given an input string `str` which consists of words and spaces, Return the length of last word in the string.
3
+
4
+
**Note:** The word is considered as a maximal substring with non-space characters.
5
+
6
+
### Examples
7
+
Example 1:
8
+
Input: "Welcome to DSA"
9
+
Output: 3
10
+
11
+
Example 2:
12
+
Input: " My pet is fluffy "
13
+
Output: 6
14
+
15
+
**Algorithmic Steps**
16
+
This problem is solved with the help of string traversal. The algorithmic approach can be summarized as follows:
17
+
18
+
1. Define the last index(i.e,`i`) of the given string and initialize length of last word(i.e, `length`) to 0.
19
+
20
+
2. Trim the ending spaces to find the last non-space character's index. This can be done through iteration from the end of a string.
21
+
22
+
3. Find the length of last word by iterating the string until the space character is found.
23
+
24
+
4. Return length of last word as an output.
25
+
26
+
**Time and Space complexity:**
27
+
This algorithm has a time complexity of `O(n)` where `n` is the length of given string. This is because we may need to iterate entire string incase of single word string(worst case scenario).
28
+
29
+
Also, it takes constant( `O(1)`) space complexity because of no additional variables required other than length output variable.
0 commit comments