Skip to content

Commit 545255e

Browse files
committed
722_Remove_Comments
1 parent c2e50f7 commit 545255e

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ I have solved quite a number of problems from several topics. See the below tabl
257257
|43| **[65. Valid Number](https://tinyurl.com/yb6zsj3b)** | [Python](https://tinyurl.com/wu6rdaw/65_Valid_Number.py), [Swift](https://tinyurl.com/wuja3c4/65_Valid_Number.swift) | [Art 1](https://tinyurl.com/ycotov9v) | HARD | A fucking difficult but important problem. Chack DFA approach |
258258
|44| **[791. Custom Sort String](https://tinyurl.com/yag8mcfd)** | [Python](https://tinyurl.com/wu6rdaw/791_Custom_Sort_String.py), [Swift](https://tinyurl.com/wuja3c4/791_Custom_Sort_String.swift) | --- | Medium | Loved this problem |
259259
|45| **[157. Read N Characters Given Read4](https://tinyurl.com/y5g49nqt)** | [Python](https://tinyurl.com/wu6rdaw/157_Read_N_Characte_Given_Read4.py), [Swift](https://tinyurl.com/wuja3c4/157_Read_N_Characte_Given_Read4.swift) | --- | Easy (Not So!) | Loved this problem |
260+
|46| **[722. Remove Comments](https://tinyurl.com/y288v3lv)** | [Python](https://tinyurl.com/wu6rdaw/722_Remove_Comments.py), [Swift](https://tinyurl.com/wuja3c4/722_Remove_Comments.swift) | [Art 1](https://tinyurl.com/y56j4p76) | Medium | |
260261

261262

262263
</p>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Source: https://tinyurl.com/y56j4p76
2+
import Foundation
3+
class Solution {
4+
func removeComments(_ source: [String]) -> [String] {
5+
6+
var result = [String]()
7+
var lineBuffer = ""
8+
var block_comment_open = false
9+
10+
for line in source {
11+
var curr_index = 0
12+
var line_arr = Array(line).map { String($0) }
13+
14+
while curr_index < line_arr.count {
15+
16+
let current_char = line_arr[curr_index]
17+
18+
if current_char == "/" && curr_index + 1 < line_arr.count && line_arr[curr_index + 1] == "/" && block_comment_open == false {
19+
curr_index = line_arr.count + 1
20+
} else if current_char == "/" && curr_index + 1 < line_arr.count && line_arr[curr_index + 1] == "*" && block_comment_open == false {
21+
block_comment_open = true
22+
curr_index += 2
23+
} else if current_char == "*" && curr_index + 1 < line_arr.count && line_arr[curr_index + 1] == "/" && block_comment_open == true {
24+
block_comment_open = false
25+
curr_index += 2
26+
} else if block_comment_open == false {
27+
lineBuffer += current_char
28+
curr_index += 1
29+
} else {
30+
curr_index += 1
31+
}
32+
}
33+
34+
if lineBuffer != "" && lineBuffer.isEmpty == false && block_comment_open == false {
35+
result.append(lineBuffer)
36+
lineBuffer = ""
37+
}
38+
}
39+
return result
40+
}
41+
}

0 commit comments

Comments
 (0)