├── .gitattributes
├── README.md
├── assets
├── sample output files
│ ├── out.epub
│ └── out.html
└── screenshots
│ ├── driver path.PNG
│ ├── epub_writer.PNG
│ ├── main.PNG
│ ├── sample_out_epub.PNG
│ └── sample_out_html.PNG
├── chapters.pickle
├── driver
├── chromedriver
└── chromedriver.exe
├── epub_writer.py
├── main.py
├── out.html
├── requirements.txt
├── track.conf
└── utils.py
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.* linguist-language=Python
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Leetcode Questions Scraper
2 |
3 | > Note: If you want to download daily updated problems you can visit my repo [Leetcode Questions](https://github.com/Bishalsarang/Leetcode-Questions) which basically checks leetcode daily and dowloads new problems if available. Here is the preview link for latest html https://bishalsarang.github.io/Leetcode-Questions/out.html
4 |
5 |
6 | Leetcode Questions Scraper is a simple scrapper built on top of Selenium that fetches all the problems from leetcode and write as html and epub files.
7 |
8 | Although leetcode doesn't provide an official API to fetch all the list of problems, we can use the API url [https://leetcode.com/api/problems/algorithms/](https://leetcode.com/api/problems/algorithms/) used by leetcode internally to fetch problems that returns a json file containing info about problems.
9 | The json file looks like this
10 | 
11 | We can build links to each problem as
12 |
13 | “https://leetcode.com/problems/" + question_title_slug
14 | After getting the problem link we can fetch the content from the page using selenium (as Leetcode is built using react where content is rendered using JS we can't use lightweight library like requests).
15 |
16 | You can download the sample html and epub containing 11 problems [here](https://github.com/Bishalsarang/Leetcode-Questions-Scrapper/tree/master/assets/sample%20output%20files).
17 |
18 | ## Requirements
19 |
20 | I have tested it on windows machine running with Google Chrome 77.0.3865.75 and chrome driver from [here](https://chromedriver.storage.googleapis.com/index.html?path=77.0.3865.40/) and put it inside driver directory.
21 | I haven't tested with Linux and Mac but you can download chrome driver for respective platform and make change to `CHROMEDRIVER_PATH` inside `main.py`
22 |
23 | Pip install all the requirements.
24 |
25 | requests==2.22.0
26 | beautifulsoup4==4.8.0
27 | selenium==3.141.0
28 | EbookLib==0.17.1
29 | colorama==0.4.1
30 |
31 |
32 | ## How to use
33 | - Clone the repo and install all the dependencies including latest google chrome and latest chrome driver
34 | - Update chrome driver path
35 | - Run the following commands to download all algorithmic problems from leetcode
36 | `python main.py`
37 | 
38 | This downloads problem contents to 2 files: *****out.html***** and ***chapters.pickle***.
39 |
40 | **NOTE:** Leetcode may temporarily block requests. If the error occurs, wait for sometime and try again or use the proxy. Don't worry, Since, the previous state is saved to ***track.conf*** file, the download resumes from where it failed.
41 |
42 | Here is how sample ***out.html*** looks like.
43 | 
44 |
45 | After ***main.py*** script executes successfully. The pickle file is automatically converted to "***Leetcode Questions.epub***".
46 |
47 | But you can also convert manually to epub with existing downloaded content with.
48 | `python epub_writer.py`
49 | 
50 |
51 | Here is how sample epub file looks like
52 | 
53 |
54 | You can download sample files from here which contains 11 problem.
55 | 1. [Sample Out.epub](https://github.com/Bishalsarang/Leetcode-Questions-Scrapper/blob/master/assets/sample%20output%20files/out.epub)
56 | 2. [Sample Out.html](https://htmlpreview.github.io/?https://github.com/Bishalsarang/Leetcode-Questions-Scraper/blob/master/assets/sample%20output%20files/out.html)
57 |
58 | ## Support
59 | If you like this project and want to support it, consider buying me a coffee!
60 |
61 | [](https://www.buymeacoffee.com/bishalsarang)
62 |
63 | Thank you for your support!
64 |
--------------------------------------------------------------------------------
/assets/sample output files/out.epub:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Bishalsarang/Leetcode-Questions-Scraper/c3d9b79572734b607b098b0adabc224a95b06530/assets/sample output files/out.epub
--------------------------------------------------------------------------------
/assets/sample output files/out.html:
--------------------------------------------------------------------------------
1 | **********
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
3 |You may assume that each input would have exactly one solution, and you may not use the same element twice.
4 |Example:
5 |Given nums = [2, 7, 11, 15], target = 9, 6 | 7 | Because nums[0] + nums[1] = 2 + 7 = 9, 8 | return [0, 1]. 9 |10 |
Given a 32-bit signed integer, reverse digits of an integer.
12 |Example 1:
13 |Input: 123 14 | Output: 321 15 |16 |
Example 2:
17 |Input: -123 18 | Output: -321 19 |20 |
Example 3:
21 |Input: 120 22 | Output: 21 23 |24 |
Note:
25 | Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
28 |Example 1:
29 |Input: 121 30 | Output: true 31 |32 |
Example 2:
33 |Input: -121 34 | Output: false 35 | Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. 36 |37 |
Example 3:
38 |Input: 10 39 | Output: false 40 | Explanation: Reads 01 from right to left. Therefore it is not a palindrome. 41 |42 |
Follow up:
43 |Coud you solve it without converting the integer to a string?
44 |Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
and M
.
Symbol Value 47 | I 1 48 | V 5 49 | X 10 50 | L 50 51 | C 100 52 | D 500 53 | M 100054 |
For example, two is written as II
in Roman numeral, just two one's added together. Twelve is written as, XII
, which is simply X
+ II
. The number twenty seven is written as XXVII
, which is XX
+ V
+ II
.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII
. Instead, the number four is written as IV
. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX
. There are six instances where subtraction is used:
I
can be placed before V
(5) and X
(10) to make 4 and 9. X
can be placed before L
(50) and C
(100) to make 40 and 90. C
can be placed before D
(500) and M
(1000) to make 400 and 900.Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
62 |Example 1:
63 |Input: "III" 64 | Output: 365 |
Example 2:
66 |Input: "IV" 67 | Output: 468 |
Example 3:
69 |Input: "IX" 70 | Output: 971 |
Example 4:
72 |Input: "LVIII" 73 | Output: 58 74 | Explanation: L = 50, V= 5, III = 3. 75 |76 |
Example 5:
77 |Input: "MCMXCIV" 78 | Output: 1994 79 | Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.80 |
Write a function to find the longest common prefix string amongst an array of strings.
82 |If there is no common prefix, return an empty string ""
.
Example 1:
84 |Input: ["flower","flow","flight"] 85 | Output: "fl" 86 |87 |
Example 2:
88 |Input: ["dog","racecar","car"] 89 | Output: "" 90 | Explanation: There is no common prefix among the input strings. 91 |92 |
Note:
93 |All given inputs are in lowercase letters a-z
.
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
97 |Note that an empty string is also considered valid.
102 |Example 1:
103 |Input: "()" 104 | Output: true 105 |106 |
Example 2:
107 |Input: "()[]{}" 108 | Output: true 109 |110 |
Example 3:
111 |Input: "(]" 112 | Output: false 113 |114 |
Example 4:
115 |Input: "([)]" 116 | Output: false 117 |118 |
Example 5:
119 |Input: "{[]}" 120 | Output: true 121 |122 |
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
124 |Example: 125 |
Input: 1->2->4, 1->3->4 126 | Output: 1->1->2->3->4->4 127 |128 |
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
130 |Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
131 |Example 1:
132 |Given nums = [1,1,2], 133 | 134 | Your function should return length =137 |2
, with the first two elements ofnums
being1
and2
respectively. 135 | 136 | It doesn't matter what you leave beyond the returned length.
Example 2:
138 |Given nums = [0,0,1,1,1,2,2,3,3,4], 139 | 140 | Your function should return length =144 |5
, with the first five elements ofnums
being modified to0
,1
,2
,3
, and4
respectively. 141 | 142 | It doesn't matter what values are set beyond the returned length. 143 |
Clarification:
145 |Confused why the returned value is an integer but your answer is an array?
146 |Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
147 |Internally you can think of this:
148 |// nums is passed in by reference. (i.e., without making a copy) 149 | int len = removeDuplicates(nums); 150 | 151 | // any modification to nums in your function would be known by the caller. 152 | // using the length returned by your function, it prints the first len elements. 153 | for (int i = 0; i < len; i++) { 154 | print(nums[i]); 155 | }156 |
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
158 |Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
159 |The order of elements can be changed. It doesn't matter what you leave beyond the new length.
160 |Example 1:
161 |Given nums = [3,2,2,3], val = 3, 162 | 163 | Your function should return length = 2, with the first two elements of nums being 2. 164 | 165 | It doesn't matter what you leave beyond the returned length. 166 |167 |
Example 2:
168 |Given nums = [0,1,2,2,3,0,4,2], val = 2, 169 | 170 | Your function should return length =175 |5
, with the first five elements ofnums
containing0
,1
,3
,0
, and 4. 171 | 172 | Note that the order of those five elements can be arbitrary. 173 | 174 | It doesn't matter what values are set beyond the returned length.
Clarification:
176 |Confused why the returned value is an integer but your answer is an array?
177 |Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
178 |Internally you can think of this:
179 |// nums is passed in by reference. (i.e., without making a copy) 180 | int len = removeElement(nums, val); 181 | 182 | // any modification to nums in your function would be known by the caller. 183 | // using the length returned by your function, it prints the first len elements. 184 | for (int i = 0; i < len; i++) { 185 | print(nums[i]); 186 | }187 |
Implement strStr().
189 |Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
190 |Example 1:
191 |Input: haystack = "hello", needle = "ll" 192 | Output: 2 193 |194 |
Example 2:
195 |Input: haystack = "aaaaa", needle = "bba" 196 | Output: -1 197 |198 |
Clarification:
199 |What should we return when needle
is an empty string? This is a great question to ask during an interview.
For the purpose of this problem, we will return 0 when needle
is an empty string. This is consistent to C's strstr() and Java's indexOf().