|
2 | 2 |
|
3 | 3 | ### Strategy (The "Why") |
4 | 4 |
|
5 | | -**Restate the problem:** We need to count vowels in the first word, then reverse each following word that has the same vowel count. Leave other words unchanged. |
| 5 | +**Restate the problem:** We need to count the vowels in the first word, then reverse each subsequent word that has the same vowel count as the first word. Words are separated by single spaces. |
6 | 6 |
|
7 | 7 | **1.1 Constraints & Complexity:** |
8 | 8 |
|
9 | | -- **Input Size:** `1 <= s.length <= 10^5` |
10 | | -- **Time Complexity:** O(n) - Single pass through string |
11 | | -- **Space Complexity:** O(n) - Store words and result |
12 | | -- **Edge Case:** Empty string or single word |
| 9 | +- **Input Size:** The string length can be up to 10^5 characters. |
| 10 | +- **Time Complexity:** O(n) where n is the string length - we iterate through the string once to split and process words. |
| 11 | +- **Space Complexity:** O(n) for storing the words array and result. |
| 12 | +- **Edge Case:** If there's only one word, no words need to be reversed, so we return the original string. |
13 | 13 |
|
14 | 14 | **1.2 High-level approach:** |
15 | 15 |
|
16 | | -Split the string into words. Count vowels in the first word. For each subsequent word, if its vowel count matches the first word's count, reverse it. Otherwise, keep it as-is. |
| 16 | +The goal is to process words sequentially: count vowels in the first word, then for each subsequent word, check if it has the same vowel count and reverse it if so. |
| 17 | + |
| 18 | + |
17 | 19 |
|
18 | 20 | **1.3 Brute force vs. optimized strategy:** |
19 | 21 |
|
20 | | -- **Brute Force:** Multiple passes - one to count vowels in first word, another to process each word. This is still O(n) but less efficient. |
21 | | -- **Optimized (Single Pass):** Split once, count vowels in first word, then process each word in one pass. This is O(n) time. |
22 | | -- **Why it's better:** We process the string efficiently in a single logical pass, making the code clear and maintainable. |
| 22 | +- **Brute Force:** Process each word multiple times, counting vowels separately for comparison and reversal, which would be less efficient. |
| 23 | +- **Optimized Strategy:** Split the string into words once, count vowels in the first word, then iterate through remaining words, counting vowels and reversing when needed. This is O(n) time. |
| 24 | +- **Optimization:** By processing words in a single pass and reusing the vowel counting logic, we avoid redundant operations. |
23 | 25 |
|
24 | 26 | **1.4 Decomposition:** |
25 | 27 |
|
26 | | -1. Split string into words |
27 | | -2. Count vowels in first word |
28 | | -3. For each subsequent word: |
29 | | - - Count its vowels |
30 | | - - If count matches first word, reverse it |
31 | | - - Otherwise, keep as-is |
32 | | -4. Join words back into string |
| 28 | +1. Split the string into words. |
| 29 | +2. Count vowels in the first word. |
| 30 | +3. For each subsequent word, count its vowels. |
| 31 | +4. If the vowel count matches the first word's count, reverse the word. |
| 32 | +5. Join all words back into a string. |
33 | 33 |
|
34 | 34 | ### Steps (The "How") |
35 | 35 |
|
36 | 36 | **2.1 Initialization & Example Setup:** |
37 | 37 |
|
38 | | -Let's use the example: `s = "cat and mice"` |
39 | | - |
40 | | -- Words: `["cat", "and", "mice"]` |
41 | | -- Vowels: `{'a', 'e', 'i', 'o', 'u'}` |
42 | | - |
43 | | -**2.2 Split into Words:** |
44 | | - |
45 | | -```python |
46 | | -words = s.split() # ["cat", "and", "mice"] |
47 | | -``` |
| 38 | +Let's use the example: `s = "cat and mice"`. |
48 | 39 |
|
49 | | -**2.3 Count Vowels in First Word:** |
| 40 | +- Words: ["cat", "and", "mice"] |
| 41 | +- First word: "cat" has 1 vowel ('a') |
50 | 42 |
|
51 | | -```python |
52 | | -first_vowel_count = sum(1 for c in words[0] if c in vowels) # "cat" has 1 vowel: 'a' |
53 | | -``` |
| 43 | +**2.2 Start Processing:** |
54 | 44 |
|
55 | | -**2.4 Process Each Subsequent Word:** |
| 45 | +We process each word after the first one, checking vowel counts and reversing when needed. |
56 | 46 |
|
57 | | -```python |
58 | | -for word in words[1:]: |
59 | | - vowel_count = sum(1 for c in word if c in vowels) |
60 | | - if vowel_count == first_vowel_count: |
61 | | - res.append(word[::-1]) # Reverse |
62 | | - else: |
63 | | - res.append(word) # Keep as-is |
64 | | -``` |
| 47 | +**2.3 Trace Walkthrough:** |
65 | 48 |
|
66 | | -For `"and"`: vowel_count = 1 (matches), so reverse → `"dna"` |
67 | | -For `"mice"`: vowel_count = 2 (doesn't match), so keep → `"mice"` |
| 49 | +| Word | Vowel Count | Matches First? | Action | Result | |
| 50 | +|------|-------------|----------------|--------|--------| |
| 51 | +| "cat" | 1 | - | Keep as is | "cat" | |
| 52 | +| "and" | 1 | Yes | Reverse | "dna" | |
| 53 | +| "mice" | 2 | No | Keep as is | "mice" | |
68 | 54 |
|
69 | | -**2.5 Join and Return:** |
| 55 | +**2.4 Increment and Loop:** |
70 | 56 |
|
71 | | -```python |
72 | | -return ' '.join(res) # "cat dna mice" |
73 | | -``` |
| 57 | +After processing all words, we join them with spaces. |
74 | 58 |
|
75 | | -**Time Complexity:** O(n) - Process each character once |
76 | | -**Space Complexity:** O(n) - Store words and result |
| 59 | +**2.5 Return Result:** |
77 | 60 |
|
| 61 | +The result is `"cat dna mice"`, where "and" (1 vowel) was reversed to "dna" because it matches the first word's vowel count, while "mice" (2 vowels) remained unchanged. |
0 commit comments