-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path39. Count words in a sentence .js
More file actions
136 lines (111 loc) · 3.63 KB
/
39. Count words in a sentence .js
File metadata and controls
136 lines (111 loc) · 3.63 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
// count words in a sentence :
// --------------------------------------------------------------------------------------------------------------::
const countWords1 = (sentence) => {
if (!sentence || sentence.trim() === "") return 0;
return sentence.trim().split(" ").length;
}
console.log(countWords1('Hello world'));
console.log(countWords1(' Hello world '));
/*
Explanation:
====================
const countWords1 = (sentence) => {
if (!sentence || sentence.trim() === "") return 0;
return sentence.trim().split(" ").length;
};
HOW IT WORKS:
- First checks for empty or null input
- trim() removes leading/trailing spaces
- split(" ") splits by single space
- Returns length of resulting array
LIMITATION: Doesn't handle multiple spaces between words properly
EXAMPLE: "Hello world" would count as 4 words (including empty strings)
*/
// ------------------------------------------------------------------------------------
const countWords3 = (sentence) => {
if (!sentence) return 0;
let count = 0;
let inWord = false;
for (let i = 0; i < sentence.length; i++) {
const char = sentence[i];
if (char !== ' ' && char !== '\t' && char !== '\n' && char !== '\r') {
if (!inWord) {
count++;
inWord = true;
}
} else {
inWord = false;
}
}
return count;
};
console.log("countWords3('Hello world'): ", countWords3('Hello world'));
console.log("countWords3(' Hello world JavaScript '): ", countWords3(' Hello world JavaScript '));
/*
Explanation:
====================
const countWords3 = (sentence) => {
if (!sentence) return 0;
let count = 0;
let inWord = false;
for (let i = 0; i < sentence.length; i++) {
const char = sentence[i];
if (char !== ' ' && char !== '\t' && char !== '\n' && char !== '\r') {
if (!inWord) {
count++;
inWord = true;
}
} else {
inWord = false;
}
}
return count;
};
HOW IT WORKS:
- Loops through each character manually
- Uses boolean flag 'inWord' to track if currently inside a word
- Increments count when entering a new word (first non-space after space)
- Considers space, tab, newline, carriage return as separators
STEP-BY-STEP EXAMPLE: "Hi there"
i=0: 'H' (not space, !inWord) → count=1, inWord=true
i=1: 'i' (not space, inWord) → continue
i=2: ' ' (space) → inWord=false
i=3: ' ' (space) → inWord=false
i=4: 't' (not space, !inWord) → count=2, inWord=true
...Result: 2 words
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1) - no extra arrays
*/
/*
Method 3 Explanation:
====================
const countWords3 = (sentence) => {
if (!sentence) return 0;
let count = 0;
let inWord = false;
for (let i = 0; i < sentence.length; i++) {
const char = sentence[i];
if (char !== ' ' && char !== '\t' && char !== '\n' && char !== '\r') {
if (!inWord) {
count++;
inWord = true;
}
} else {
inWord = false;
}
}
return count;
};
HOW IT WORKS:
- Loops through each character manually
- Uses boolean flag 'inWord' to track if currently inside a word
- Increments count when entering a new word (first non-space after space)
- Considers space, tab, newline, carriage return as separators
STEP-BY-STEP EXAMPLE: "Hi there"
i=0: 'H' (not space, !inWord) → count=1, inWord=true
i=1: 'i' (not space, inWord) → continue
i=2: ' ' (space) → inWord=false
i=3: ' ' (space) → inWord=false
i=4: 't' (not space, !inWord) → count=2, inWord=true
...Result: 2 words
*/