Skip to content

Commit 87ab876

Browse files
committed
Add length of last word problem
1 parent 806b8b3 commit 87ab876

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ List of Programs related to data structures and algorithms
7171
| 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 |
7272
| 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 |
7373
| 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 |
7475

7576
### Dynamic programming
7677

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package java1.algorithms.strings.lastWordLength;
2+
3+
public class LastWordLength {
4+
private static int lengthOfLastWord(String str){
5+
int i = str.length()-1, length = 0;
6+
7+
while(str.charAt(i) == ' '){
8+
i--;
9+
}
10+
11+
while (i >= 0 && str.charAt(i) != ' ') {
12+
i--;
13+
length++;
14+
}
15+
16+
return length;
17+
}
18+
public static void main(String[] args) {
19+
String str1 = "Welcome to DSA";
20+
String str2 = " My pet is fluffy ";
21+
22+
System.out.println(lengthOfLastWord(str1));
23+
System.out.println(lengthOfLastWord(str2));
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
**Description:**
2+
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function lengthOfLastWord(str){
2+
let i = str.length-1, length = 0;
3+
4+
while(str[i] === ' '){
5+
i--;
6+
}
7+
8+
while(i >=0 && str[i] !== ' '){
9+
length++;
10+
i--;
11+
}
12+
13+
return length;
14+
}
15+
16+
let str1 = "Welcome to DSA";
17+
let str2 = " My pet is fluffy ";
18+
19+
console.log(lengthOfLastWord(str1));
20+
console.log(lengthOfLastWord(str2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
**Description:**
2+
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

Comments
 (0)