Skip to content

Commit 7a4e8bb

Browse files
add file to read and update companies list
1 parent 409d9e3 commit 7a4e8bb

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Solution.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.io.IOException;
2+
import java.nio.charset.StandardCharsets;
3+
import java.nio.file.Files;
4+
import java.nio.file.Path;
5+
import java.util.ArrayList;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
9+
public class Solution {
10+
public static void main(String[] args) throws IOException {
11+
writeFile();
12+
}
13+
14+
public static void writeFile() throws IOException {
15+
16+
Path FILE_PATH = Path.of("D:\\projects\\Leetcode\\README.md");
17+
List<String> fileContent = new ArrayList<>(Files.readAllLines(FILE_PATH, StandardCharsets.UTF_8));
18+
19+
Path COMPANY = Path.of("D:\\projects\\Leetcode\\companies.txt");
20+
List<String> companyContent = new ArrayList<>(Files.readAllLines(COMPANY, StandardCharsets.UTF_8));
21+
22+
for (String queNo : new HashSet<>(companyContent)) {
23+
queNo = "_" + queNo.trim() + ".java";
24+
25+
for (int i = 0; i < fileContent.size(); i++) {
26+
if (fileContent.get(i).contains(queNo)) {
27+
String updatedLine = updateLine(fileContent.get(i));
28+
fileContent.set(i, updatedLine);
29+
break;
30+
}
31+
}
32+
}
33+
34+
Files.write(FILE_PATH, fileContent, StandardCharsets.UTF_8);
35+
}
36+
37+
private static String updateLine(String s) {
38+
String companyName = "Snapchat,Square,Twitter,Uber,Yelp";
39+
40+
String[] splits = s.split("\\|");
41+
if (splits[6].trim().isEmpty()) {
42+
splits[6] = companyName;
43+
} else {
44+
splits[6] = splits[6] + "<br>" + companyName;
45+
}
46+
return String.join("|", splits);
47+
}
48+
}

0 commit comments

Comments
 (0)