-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLengthOfLastWord.java
More file actions
138 lines (104 loc) · 3.82 KB
/
LengthOfLastWord.java
File metadata and controls
138 lines (104 loc) · 3.82 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package Algorithms.Strings;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 06 April 2025
* @link 58. Length of Last Word <a href="https://leetcode.com/problems/length-of-last-word/">LeetCode link</a>
* @topics String, String methods
split(" ") vs split("\\s+") vs split("\\W+")
split(" ") -> splits on whitespace
split("\\s+") -> splits on one or more spaces
split("\\W+") -> splits on one or more non-word characters
Example 1:
Input: s = " Hello World, ok ok "
s.split(" ") => String[8] { "", "", "", "", "Hello", "World,", "ok", "ok" }
s.trim().split(" ") String[4] { "Hello", "World,", "ok", "ok" }
s.split("\\s") => String[8] { "", "", "", "", "Hello", "World,", "ok", "ok" }
s.split("\\s+") => String[5] { "", "Hello", "World,", "ok", "ok" }
s.split("\\W") => String[9] { "", "", "", "", "Hello", "World", "", "ok", "ok" }
s.split("\\W+") => String[5] { "", "Hello", "World", "ok", "ok"
s.trim() => "Hello World, ok ok"
s.strip() => "Hello World, ok ok" ---> Java 11
NOTE: "a_b c".split("\\W") => String[2]{"a_b", "c"} --> cause _ is a word character. so, a_b is a word
JAVA REGEX META CHARACTERS:
----------------------------
1. Characters with Special Meaning:
. (Dot) Matches Any Character
\ (Backslash) Escape Character
^ Start of line/input
$ End of line/input
* 0 or more repetitions
+ 1 or more repetitions
? 0 or 1 repetition, or makes quantifier lazy
[] Character class
() Capturing group
{} Quantifier: exact, min, or min,max
2. Predefined Character Classes:
\s -> whitespace [\t\n\x0B\f\r]
\S -> non-whitespace
\w -> word character
\W -> non-word character
\d -> digit character [0-9]
\D -> non-digit character [^0-9]
\b -> word boundary
\B -> non-word boundary
*/
public class LengthOfLastWord {
public static void main(String[] args) {
String s = "Hello World";
System.out.printf("lengthOfLastWord => %d", lengthOfLastWord(s));
}
public static int lengthOfLastWord(String s) {
String[] words = s.split(" "); // or s.trim().split(" "); or s.split("\\s+"); or s.split("\\W");
return words[words.length - 1].length();
}
public int lengthOfLastWordMyApproach(String s) {
int res = 0;
for(int i=s.length()-1; i>=0; i--) {
if(s.charAt(i) == ' ') {
if(res == 0) continue;
else break;
}
res++;
}
return res;
}
public static int lengthOfLastWord2(String s) {
int length = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == ' ') {
if (length != 0) return length;
} else {
length++;
}
}
return length;
}
public static int lengthOfLastWord3(String s) {
int length = 0;
int end = s.length() - 1;
while(end >= 0 && s.charAt(end) == ' ') end--;
for(int i=0; i<=end; i++) {
if(s.charAt(i) == ' ') {
length = 0;
} else {
length++;
}
}
return length;
}
public static int lengthOfLastWord4(String s) {
return s.trim().length() - s.trim().lastIndexOf(' ') - 1;
}
public static int lengthOfLastWord5(String s) {
int i = s.length() - 1;
while (i >= 0 && s.charAt(i) == ' ') i--; // skip trailing spaces
int j = i;
while (j >= 0 && s.charAt(j) != ' ') j--; // find beginning of last word
return i - j;
}
public static int lengthOfLastWord6(String s) {
int i = s.length() - 1;
while (i >= 0 && s.charAt(i) == ' ') i--; // skip trailing spaces
return s.substring(i + 1).length();
}
}