|
| 1 | +--- |
| 2 | +id: number-of-valid-words-in-a-sentence |
| 3 | +title: Number of Valid Words in a Sentence |
| 4 | +sidebar_label: 2047-Number of Valid Words in a Sentence |
| 5 | +tags: |
| 6 | + - Valid Words |
| 7 | + - Brute Force |
| 8 | + - Optimized |
| 9 | + - LeetCode |
| 10 | + - Java |
| 11 | + - Python |
| 12 | + - C++ |
| 13 | +description: "This is a solution to the Number of Valid Words problem on LeetCode." |
| 14 | +sidebar_position: 2 |
| 15 | +--- |
| 16 | + |
| 17 | +In this tutorial, we will solve the Number of Valid Words problem using two different approaches: brute force and optimized. We will provide the implementation of the solution in C++, Java, and Python. |
| 18 | + |
| 19 | +## Problem Description |
| 20 | + |
| 21 | +Given a sentence, count the number of valid words. A valid word is defined by the following rules: |
| 22 | +1. It only contains lowercase letters, hyphens, and/or punctuation (no digits). |
| 23 | +2. There is at most one hyphen ('-'). If present, it must be surrounded by lowercase characters ("a-b" is valid, but "-ab" and "ab-" are not valid). |
| 24 | +3. There is at most one punctuation mark ('!', '.', ','). If present, it must be at the end of the token ("ab,", "cd!", and "." are valid, but "a!b" and "c.," are not valid). |
| 25 | + |
| 26 | +### Examples |
| 27 | + |
| 28 | +**Example 1:** |
| 29 | + |
| 30 | +``` |
| 31 | +Input: sentence = "cat and dog" |
| 32 | +Output: 3 |
| 33 | +Explanation: The valid words in the sentence are "cat", "and", and "dog". |
| 34 | +``` |
| 35 | + |
| 36 | +**Example 2:** |
| 37 | + |
| 38 | +``` |
| 39 | +Input: sentence = "!this 1-s b8d!" |
| 40 | +Output: 0 |
| 41 | +Explanation: There are no valid words in the sentence. "!this" is invalid because it starts with a punctuation mark. "1-s" and "b8d" are invalid because they contain digits. |
| 42 | +
|
| 43 | +``` |
| 44 | + |
| 45 | +**Example 3:** |
| 46 | + |
| 47 | +``` |
| 48 | +Input: sentence = "alice and bob are playing stone-game10" |
| 49 | +Output: 5 |
| 50 | +Explanation: The valid words in the sentence are "alice", "and", "bob", "are", and "playing". "stone-game10" is invalid because it contains digits. |
| 51 | +``` |
| 52 | + |
| 53 | +### Constraints |
| 54 | + |
| 55 | +- `1 <= sentence.length <= 1000` |
| 56 | +- `sentence` only contains lowercase English letters, digits, ' ', '-', '!', '.', and ','. |
| 57 | +- There will be at least 1 token. |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## Solution for Number of Valid Words Problem |
| 62 | + |
| 63 | +### Intuition and Approach |
| 64 | + |
| 65 | +The problem can be solved using a brute force approach or an optimized approach. The brute force approach directly iterates through the string and checks each token for validity, while the optimized approach streamlines the validation process. |
| 66 | + |
| 67 | +<Tabs> |
| 68 | +<tabItem value="Brute Force" label="Brute Force"> |
| 69 | + |
| 70 | +### Approach 1: Brute Force |
| 71 | + |
| 72 | +The brute force approach iterates through each token of the sentence, validates it according to the given rules, and counts the number of valid tokens. |
| 73 | + |
| 74 | +#### Code in Different Languages |
| 75 | + |
| 76 | +<Tabs> |
| 77 | +<TabItem value="C++" label="C++" default> |
| 78 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 79 | + |
| 80 | +```cpp |
| 81 | +#include <string> |
| 82 | +#include <vector> |
| 83 | +#include <sstream> |
| 84 | + |
| 85 | +class Solution { |
| 86 | +public: |
| 87 | + int countValidWords(std::string sentence) { |
| 88 | + std::istringstream iss(sentence); |
| 89 | + std::string word; |
| 90 | + int validWordCount = 0; |
| 91 | + |
| 92 | + while (iss >> word) { |
| 93 | + if (isValid(word)) { |
| 94 | + validWordCount++; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return validWordCount; |
| 99 | + } |
| 100 | + |
| 101 | +private: |
| 102 | + bool isValid(const std::string& word) { |
| 103 | + int hyphenCount = 0; |
| 104 | + int punctuationCount = 0; |
| 105 | + |
| 106 | + for (int i = 0; i < word.length(); i++) { |
| 107 | + if (isdigit(word[i])) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + if (word[i] == '-') { |
| 111 | + hyphenCount++; |
| 112 | + if (hyphenCount > 1 || i == 0 || i == word.length() - 1 || !islower(word[i-1]) || !islower(word[i+1])) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + } |
| 116 | + if (word[i] == '!' || word[i] == '.' || word[i] == ',') { |
| 117 | + punctuationCount++; |
| 118 | + if (punctuationCount > 1 || i != word.length() - 1) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return true; |
| 125 | + } |
| 126 | +}; |
| 127 | +``` |
| 128 | +
|
| 129 | +</TabItem> |
| 130 | +<TabItem value="Java" label="Java"> |
| 131 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 132 | +
|
| 133 | +```java |
| 134 | +class Solution { |
| 135 | + public int countValidWords(String sentence) { |
| 136 | + String[] tokens = sentence.split("\\s+"); |
| 137 | + int validWordCount = 0; |
| 138 | + |
| 139 | + for (String token : tokens) { |
| 140 | + if (isValid(token)) { |
| 141 | + validWordCount++; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return validWordCount; |
| 146 | + } |
| 147 | + |
| 148 | + private boolean isValid(String word) { |
| 149 | + int hyphenCount = 0; |
| 150 | + int punctuationCount = 0; |
| 151 | + |
| 152 | + for (int i = 0; i < word.length(); i++) { |
| 153 | + char c = word.charAt(i); |
| 154 | + |
| 155 | + if (Character.isDigit(c)) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + if (c == '-') { |
| 159 | + hyphenCount++; |
| 160 | + if (hyphenCount > 1 || i == 0 || i == word.length() - 1 || !Character.isLowerCase(word.charAt(i - 1)) || !Character.isLowerCase(word.charAt(i + 1))) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + } |
| 164 | + if (c == '!' || c == '.' || c == ',') { |
| 165 | + punctuationCount++; |
| 166 | + if (punctuationCount > 1 || i != word.length() - 1) { |
| 167 | + return false; |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return true; |
| 173 | + } |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +</TabItem> |
| 178 | +<TabItem value="Python" label="Python"> |
| 179 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 180 | + |
| 181 | +```python |
| 182 | +class Solution: |
| 183 | + def countValidWords(self, sentence: str) -> int: |
| 184 | + tokens = sentence.split() |
| 185 | + valid_word_count = 0 |
| 186 | + |
| 187 | + for token in tokens: |
| 188 | + if self.is_valid(token): |
| 189 | + valid_word_count += 1 |
| 190 | + |
| 191 | + return valid_word_count |
| 192 | + |
| 193 | + def is_valid(self, word: str) -> bool: |
| 194 | + hyphen_count = 0 |
| 195 | + punctuation_count = 0 |
| 196 | + |
| 197 | + for i, c in enumerate(word): |
| 198 | + if c.isdigit(): |
| 199 | + return False |
| 200 | + if c == '-': |
| 201 | + hyphen_count += 1 |
| 202 | + if hyphen_count > 1 or i == 0 or i == len(word) - 1 or not (word[i - 1].islower() and word[i + 1].islower()): |
| 203 | + return False |
| 204 | + if c in "!.,": |
| 205 | + punctuation_count += 1 |
| 206 | + if punctuation_count > 1 or i != len(word) - 1: |
| 207 | + return False |
| 208 | + |
| 209 | + return True |
| 210 | +``` |
| 211 | + |
| 212 | +</TabItem> |
| 213 | +</Tabs> |
| 214 | + |
| 215 | +#### Complexity Analysis |
| 216 | + |
| 217 | +- Time Complexity: $O(n)$ |
| 218 | +- Space Complexity: $O(n)$ |
| 219 | +- Where `n` is the length of the sentence. |
| 220 | +- The time complexity is $O(n)$ because we iterate through each character in the sentence once. |
| 221 | +- The space complexity is $O(n)$ because we store the result in a new string or list of strings. |
| 222 | + |
| 223 | +</tabItem> |
| 224 | +<tabItem value="Optimized" label="Optimized"> |
| 225 | + |
| 226 | +### Approach 2: Optimized Approach |
| 227 | + |
| 228 | +The optimized approach uses similar logic but may include improvements such as pre-checking token conditions or using efficient string methods. |
| 229 | + |
| 230 | +#### Code in Different Languages |
| 231 | + |
| 232 | +<Tabs> |
| 233 | +<TabItem value="C++" label="C++" default> |
| 234 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 235 | + |
| 236 | +```cpp |
| 237 | +#include <string> |
| 238 | +#include <vector> |
| 239 | +#include <sstream> |
| 240 | + |
| 241 | +class Solution { |
| 242 | +public: |
| 243 | + int countValidWords(std::string sentence) { |
| 244 | + std::istringstream iss(sentence); |
| 245 | + std::string word; |
| 246 | + int validWordCount = 0; |
| 247 | + |
| 248 | + while (iss >> word) { |
| 249 | + if (isValid(word)) { |
| 250 | + validWordCount++; |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + return validWordCount; |
| 255 | + } |
| 256 | + |
| 257 | +private: |
| 258 | + bool isValid(const std::string& word) { |
| 259 | + int hyphenCount = 0; |
| 260 | + int punctuationCount = 0; |
| 261 | + int n = word.length(); |
| 262 | + |
| 263 | + for (int i = 0; i < n; i++) { |
| 264 | + char c = word[i]; |
| 265 | + |
| 266 | + if (isdigit(c)) { |
| 267 | + return false; |
| 268 | + } |
| 269 | + if (c == '-') { |
| 270 | + hyphenCount++; |
| 271 | + if (hyphenCount > 1 || i == 0 || i == n - 1 || !islower(word[i-1]) || !islower(word[i+1])) { |
| 272 | + return false; |
| 273 | + } |
| 274 | + } |
| 275 | + if (c == '!' || c == '.' || c == ',') { |
| 276 | + punctuationCount++; |
| 277 | + if (punctuationCount > 1 || i != n - 1) { |
| 278 | + return false; |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + return true; |
| 284 | + } |
| 285 | +}; |
| 286 | +``` |
| 287 | +
|
| 288 | +</TabItem> |
| 289 | +<TabItem value="Java" label="Java"> |
| 290 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 291 | +
|
| 292 | +```java |
| 293 | +class Solution { |
| 294 | + public int countValidWords(String sentence) { |
| 295 | + String[] tokens = sentence.split("\\s+"); |
| 296 | + int validWordCount = 0; |
| 297 | + |
| 298 | + for (String token : tokens) { |
| 299 | + if (isValid(token)) { |
| 300 | + validWordCount++; |
| 301 | + } |
| 302 | + } |
| 303 | + |
| 304 | + return validWordCount; |
| 305 | + } |
| 306 | + |
| 307 | + private boolean isValid(String word) { |
| 308 | + int hyphenCount = 0; |
| 309 | + int punctuation |
| 310 | +
|
| 311 | + Count = 0; |
| 312 | + int n = word.length(); |
| 313 | + |
| 314 | + for (int i = 0; i < n; i++) { |
| 315 | + char c = word.charAt(i); |
| 316 | + |
| 317 | + if (Character.isDigit(c)) { |
| 318 | + return false; |
| 319 | + } |
| 320 | + if (c == '-') { |
| 321 | + hyphenCount++; |
| 322 | + if (hyphenCount > 1 || i == 0 || i == n - 1 || !Character.isLowerCase(word.charAt(i - 1)) || !Character.isLowerCase(word.charAt(i + 1))) { |
| 323 | + return false; |
| 324 | + } |
| 325 | + } |
| 326 | + if (c == '!' || c == '.' || c == ',') { |
| 327 | + punctuationCount++; |
| 328 | + if (punctuationCount > 1 || i != n - 1) { |
| 329 | + return false; |
| 330 | + } |
| 331 | + } |
| 332 | + } |
| 333 | + |
| 334 | + return true; |
| 335 | + } |
| 336 | +} |
| 337 | +``` |
| 338 | + |
| 339 | +</TabItem> |
| 340 | +<TabItem value="Python" label="Python"> |
| 341 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 342 | + |
| 343 | +```python |
| 344 | +class Solution: |
| 345 | + def countValidWords(self, sentence: str) -> int: |
| 346 | + tokens = sentence.split() |
| 347 | + valid_word_count = 0 |
| 348 | + |
| 349 | + for token in tokens: |
| 350 | + if self.is_valid(token): |
| 351 | + valid_word_count += 1 |
| 352 | + |
| 353 | + return valid_word_count |
| 354 | + |
| 355 | + def is_valid(self, word: str) -> bool: |
| 356 | + hyphen_count = 0 |
| 357 | + punctuation_count = 0 |
| 358 | + n = len(word) |
| 359 | + |
| 360 | + for i, c in enumerate(word): |
| 361 | + if c.isdigit(): |
| 362 | + return False |
| 363 | + if c == '-': |
| 364 | + hyphen_count += 1 |
| 365 | + if hyphen_count > 1 or i == 0 or i == n - 1 or not (word[i - 1].islower() and word[i + 1].islower()): |
| 366 | + return False |
| 367 | + if c in "!.,": |
| 368 | + punctuation_count += 1 |
| 369 | + if punctuation_count > 1 or i != n - 1: |
| 370 | + return False |
| 371 | + |
| 372 | + return True |
| 373 | +``` |
| 374 | + |
| 375 | +</TabItem> |
| 376 | +</Tabs> |
| 377 | + |
| 378 | +#### Complexity Analysis |
| 379 | + |
| 380 | +- Time Complexity: $O(n)$ |
| 381 | +- Space Complexity: $O(n)$ |
| 382 | +- Where `n` is the length of the sentence. |
| 383 | +- The time complexity is $O(n)$ because we iterate through each character in the sentence once. |
| 384 | +- The space complexity is $O(n)$ because we store the result in a new string or list of strings. |
| 385 | + |
| 386 | +</tabItem> |
| 387 | +</Tabs> |
| 388 | + |
| 389 | +--- |
| 390 | + |
| 391 | +<h2>Authors:</h2> |
| 392 | + |
| 393 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 394 | +{['ImmidiSivani'].map(username => ( |
| 395 | + <Author key={username} username={username} /> |
| 396 | +))} |
| 397 | +</div> |
0 commit comments