Skip to content

Commit 71efe7d

Browse files
Merge branch 'neetcode-gh:main' into main
2 parents 0e203a7 + a5ea5e4 commit 71efe7d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+8683
-48
lines changed

articles/add-bold-tag-in-string.md

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
## 1. Mark Bold Characters
2+
3+
::tabs-start
4+
5+
```python
6+
class Solution:
7+
def addBoldTag(self, s: str, words: List[str]) -> str:
8+
n = len(s)
9+
bold = [False] * n
10+
11+
for word in words:
12+
start = s.find(word)
13+
while start != -1:
14+
for i in range(start, start + len(word)):
15+
bold[i] = True
16+
17+
start = s.find(word, start + 1)
18+
19+
open_tag = "<b>"
20+
close_tag = "</b>"
21+
ans = []
22+
23+
for i in range(n):
24+
if bold[i] and (i == 0 or not bold[i - 1]):
25+
ans.append(open_tag)
26+
27+
ans.append(s[i])
28+
29+
if bold[i] and (i == n - 1 or not bold[i + 1]):
30+
ans.append(close_tag)
31+
32+
return "".join(ans)
33+
```
34+
35+
```java
36+
class Solution {
37+
public String addBoldTag(String s, String[] words) {
38+
int n = s.length();
39+
boolean[] bold = new boolean[n];
40+
41+
for (String word: words) {
42+
int start = s.indexOf(word);
43+
while (start != -1) {
44+
for (int i = start; i < start + word.length(); i++) {
45+
bold[i] = true;
46+
}
47+
48+
start = s.indexOf(word, start + 1);
49+
}
50+
}
51+
52+
String openTag = "<b>";
53+
String closeTag = "</b>";
54+
StringBuilder ans = new StringBuilder();
55+
56+
for (int i = 0; i < n; i++) {
57+
if (bold[i] && (i == 0 || !bold[i - 1])) {
58+
ans.append(openTag);
59+
}
60+
61+
ans.append(s.charAt(i));
62+
63+
if (bold[i] && (i == n - 1 || !bold[i + 1])) {
64+
ans.append(closeTag);
65+
}
66+
}
67+
68+
return ans.toString();
69+
}
70+
}
71+
```
72+
73+
```cpp
74+
class Solution {
75+
public:
76+
string addBoldTag(string s, vector<string>& words) {
77+
int n = s.size();
78+
vector<bool> bold(n);
79+
80+
for (string word: words) {
81+
int start = s.find(word);
82+
while (start != -1) {
83+
for (int i = start; i < start + word.size(); i++) {
84+
bold[i] = true;
85+
}
86+
87+
start = s.find(word, start + 1);
88+
}
89+
}
90+
91+
string openTag = "<b>";
92+
string closeTag = "</b>";
93+
string ans = "";
94+
95+
for (int i = 0; i < n; i++) {
96+
if (bold[i] && (i == 0 || !bold[i - 1])) {
97+
ans += openTag;
98+
}
99+
100+
ans += s[i];
101+
102+
if (bold[i] && (i == n - 1 || !bold[i + 1])) {
103+
ans += closeTag;
104+
}
105+
}
106+
107+
return ans;
108+
}
109+
};
110+
```
111+
112+
```javascript
113+
class Solution {
114+
/**
115+
* @param {string} s
116+
* @param {string[]} words
117+
* @return {string}
118+
*/
119+
addBoldTag(s, words) {
120+
const n = s.length;
121+
const bold = new Array(n).fill(false);
122+
123+
for (const word of words) {
124+
let start = s.indexOf(word);
125+
while (start !== -1) {
126+
for (let i = start; i < start + word.length; i++) {
127+
bold[i] = true;
128+
}
129+
start = s.indexOf(word, start + 1);
130+
}
131+
}
132+
133+
const openTag = "<b>";
134+
const closeTag = "</b>";
135+
const ans = [];
136+
137+
for (let i = 0; i < n; i++) {
138+
if (bold[i] && (i === 0 || !bold[i - 1])) {
139+
ans.push(openTag);
140+
}
141+
ans.push(s[i]);
142+
if (bold[i] && (i === n - 1 || !bold[i + 1])) {
143+
ans.push(closeTag);
144+
}
145+
}
146+
147+
return ans.join("");
148+
}
149+
}
150+
```
151+
152+
::tabs-end
153+
154+
### Time & Space Complexity
155+
The time complexity may differ between languages. It is dependent on how the built-in method is implemented.
156+
For this analysis, we will assume that we are using Java.
157+
158+
- Time complexity: $O(m \cdot (n^2 \cdot k - n \cdot k^2))$
159+
- Space complexity: $O(n)$
160+
161+
> Where $n$ is `s.length`, $m$ is `words.length`, and $k$ is the average length of the words.

articles/anagram-groups.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
## 1. Sorting
22

3+
### Intuition
4+
5+
Anagrams become identical when their characters are sorted.
6+
For example, `"eat"`, `"tea"`, and `"ate"` all become `"aet"` after sorting.
7+
By using the sorted version of each string as a key, we can group all anagrams together.
8+
Strings that share the same sorted form must be anagrams, so placing them in the same group is both natural and efficient.
9+
10+
### Algorithm
11+
12+
1. Create a hash map where each key is the sorted version of a string, and the value is a list of strings belonging to that anagram group.
13+
2. Iterate through each string in the input list:
14+
- Sort the characters of the string to form a key.
15+
- Append the original string to the list corresponding to this key.
16+
3. After processing all strings, return all values from the hash map, which represent the grouped anagrams.
17+
318
::tabs-start
419

520
```python
@@ -153,6 +168,23 @@ class Solution {
153168

154169
## 2. Hash Table
155170

171+
### Intuition
172+
173+
Instead of sorting each string, we can represent every string by the frequency of its characters.
174+
Since the problem uses lowercase English letters, a fixed-size array of length `26` can capture how many times each character appears.
175+
Two strings are anagrams if and only if their frequency arrays are identical.
176+
By using this frequency array (converted to a tuple so it can be a dictionary key), we can group all strings that share the same character counts.
177+
178+
### Algorithm
179+
180+
1. Create a hash map where each key is a `26`-length tuple representing character frequencies, and each value is a list of strings belonging to that anagram group.
181+
2. For each string in the input:
182+
- Initialize a frequency array of size `26` with all zeros.
183+
- For each character in the string, increment the count at the corresponding index.
184+
- Convert the frequency array to a tuple and use it as the key.
185+
- Append the string to the list associated with this key.
186+
3. After processing all strings, return all the lists stored in the hash map.
187+
156188
::tabs-start
157189

158190
```python

0 commit comments

Comments
 (0)