Repository with my solutions to various LeetCode problems.
To solve these problems I use functions from Golang Standard Library where applicable...
My main focus is with these areas: Arrays, Hash Tables, Strings, Dynamic Programming.
Read this document for all tips and tricks that are useful for solving these problems.
π© Easy: 263
π¨ Medium: 40
# | Title | Solution | Level |
---|---|---|---|
1 | Two Sum | link | π© |
3 | Longest Substring Without Repeating Characters | link | π¨ |
7 | Reverse Integer | link | π¨ |
9 | Palindrome Number | link | π© |
13 | Roman to Integer | link | π© |
14 | Longest Common Prefix | link | π© |
20 | Valid Parentheses | link | π© |
21 | Merge Two Sorted Lists | link | π© |
26 | Remove Duplicates from Sorted Array | link | π© |
27 | Remove Element | link | π© |
28 | Find the Index of the First Occurrence in a String | link | π© |
35 | Search Insert Position | link | π© |
46 | Permutations | link | π© |
58 | Length of Last Word | link | π© |
66 | Plus One | link | π© |
67 | Add Binary | link | π© |
69 | Sqrt(x) | link | π© |
70 | Climbing Stairs | link | π© |
75 | Sort Colors | link | π¨ |
78 | Subsets | link | π¨ |
83 | Remove Duplicates from Sorted List | link | π© |
88 | Merge Sorted Array | link | π© |
94 | Binary Tree Inorder Traversal | link | π© |
100 | Same Tree | link | π© |
101 | Symmetric Tree | link | π© |
104 | Maximum Depth of Binary Tree | link | π© |
118 | Pascal's Triangle | link | π© |
119 | Pascal's Triangle II | link | π© |
121 | Best Time to Buy and Sell Stock | link | π© |
125 | Valid Palindrome | link | π© |
136 | Single Number | link | π© |
151 | Reverse Words in a String | link | π¨ |
167 | Two Sum II | link | π¨ |
168 | Excel Sheet Column Title | link | π© |
169 | Majority Element | link | π© |
171 | Excel Sheet Column Number | link | π© |
190 | Reverse Bits | link | π© |
191 | Number of 1 Bits | link | π© |
198 | House Robber | link | π© |
205 | Isomorphic Strings | link | π© |
206 | Reverse Linked List | link | π© |
217 | Contains Duplicate | link | π© |
219 | Contains Duplicate II | link | π© |
228 | Summary Ranges | link | π© |
242 | Valid Anagram | link | π© |
257 | Binary Tree Paths | link | π© |
258 | Add Digits | link | π© |
268 | Missing Number | link | π© |
283 | Move Zeroes | link | π© |
290 | Word Pattern | link | π© |
303 | Range Sum Query - Immutable | link | π© |
338 | Counting Bits | link | π© |
344 | Reverse String | link | π© |
345 | Reverse Vowels of a String | link | π© |
349 | Intersection of Two Arrays | link | π© |
350 | Intersection of Two Arrays II | link | π© |
383 | Ransom Note | link | π© |
387 | First Unique Character in a String | link | π© |
389 | Find the Difference | link | π© |
392 | Is Subsequence | link | π© |
401 | Binary Watch | link | π© |
405 | Convert a Number to Hexadecimal | link | π© |
412 | Fizz Buzz | link | π© |
415 | Add Strings | link | π© |
434 | Number of Segments in a String | link | π© |
442 | Find All Duplicates in an Array | link | π© |
448 | Find All Numbers Disappeared in an Array | link | π© |
451 | Sort Characters By Frequency | link | π¨ |
455 | Assign Cookies | link | π© |
459 | Repeated Substring Pattern | link | π© |
463 | Island Perimeter | link | π© |
476 | Number Complement | link | π© |
482 | License Key Formatting | link | π© |
485 | Max Consecutive Ones | link | π© |
500 | Keyboard Row | link | π© |
509 | Fibonacci Number | link | π© |
520 | Detect Capital | link | π© |
520 | Detect Capital | link | π© |
535 | Encode and Decode TinyURL | link | π¨ |
551 | Student Attendance Record I | link | π© |
557 | Reverse Words in a String III | link | π© |
599 | Minimum Index Sum of Two Lists | link | π© |
657 | Robot Return to Origin | link | π© |
682 | Baseball Game | link | π© |
693 | Binary Number with Alternating Bits | link | π© |
709 | To Lower Case | link | π© |
724 | Find Pivot Index | link | π© |
746 | Min Cost Climbing Stairs | link | π© |
762 | Prime Number of Set Bits in Binary Representation | link | π© |
771 | Jewels and Stones | link | π© |
796 | Rotate String | link | π© |
804 | Unique Morse Code Words | link | π© |
806 | Number of Lines To Write String | link | π© |
811 | Subdomain Visit Count | link | π¨ |
819 | Most Common Word | link | π© |
821 | Shortest Distance to a Character | link | π© |
824 | Goat Latin | link | π© |
830 | Positions of Large Groups | link | π© |
832 | Flipping an Image | link | π© |
844 | Backspace String Compare | link | π© |
861 | Score After Flipping Matrix | link | π¨ |
868 | Binary Gap | link | π© |
884 | Uncommon Words from Two Sentences | link | π© |
944 | Delete Columns to Make Sorted | link | π© |
961 | N-Repeated Element in Size 2N Array | link | π© |
1002 | Find Common Characters | link | π© |
1047 | Remove All Adjacent Duplicates In String | link | π© |
1051 | Height Checker | link | π© |
1078 | Occurrences After Bigram | link | π© |
1089 | Duplicate Zeros | link | π© |
1108 | Defanging an IP Address | link | π© |
1154 | Day of the Year | link | π© |
1189 | Maximum Number of Balloons | link | π© |
1207 | Unique Number of Occurrences | link | π© |
1221 | Split a String in Balanced Strings | link | π© |
1275 | Find Winner on a Tic Tac Toe Game | link | π© |
1282 | Group the People Given the Group Size They Belong To | link | π¨ |
1287 | Element Appearing More Than 25% In Sorted Array | link | π© |
1304 | Find N Unique Integers Sum up to Zero | link | π© |
1309 | Decrypt String from Alphabet to Integer Mapping | link | π© |
1310 | XOR Queries of a Subarray | link | π¨ |
1313 | Decompress Run-Length Encoded List | link | π© |
1331 | Rank Transform of an Array | link | π© |
1346 | Check If N and Its Double Exist | link | π© |
1351 | Count Negative Numbers in a Sorted Matrix | link | π© |
1360 | Number of Days Between Two Dates | link | π© |
1365 | How Many Numbers Are Smaller Than the Current Number | link | π© |
1380 | Lucky Numbers in a Matrix | link | π© |
1381 | Design a Stack with Increment Operation | link | π¨ |
1389 | Create Target Array in the Given Order | link | π© |
1408 | String Matching in an Array | link | π© |
1413 | Minimum Value to Get Positive Step by Step Sum | link | π© |
1431 | Kids With the Greatest Number of Candies | link | π© |
1436 | Destination City | link | π© |
1455 | Check If a Word Occurs As a Prefix | link | π© |
1470 | Shuffle the Array | link | π© |
1476 | Subrectangle Queries | link | π¨ |
1480 | Running Sum of 1d Array | link | π© |
1486 | XOR Operation in an Array | link | π© |
1496 | Path Crossing | link | π© |
1507 | Reformat Date | link | π© |
1512 | Number of Good Pairs | link | π© |
1518 | Water Bottles | link | π© |
1528 | Shuffle String | link | π© |
1550 | Three Consecutive Odds | link | π© |
1556 | Thousand Separator | link | π© |
1561 | Maximum Number of Coins You Can Get | link | π¨ |
1572 | Matrix Diagonal Sum | link | π© |
1588 | Sum of All Odd Length Subarrays | link | π© |
1592 | Rearrange Spaces Between Words | link | π© |
1614 | Maximum Nesting Depth of the Parentheses | link | π© |
1624 | Largest Substring Between Two Equal Characters | link | π© |
1630 | Arithmetic Subarrays | link | π¨ |
1637 | Widest Vertical Area Between Two Points Containing No Points | link | π© |
1662 | Check If Two String Arrays are Equivalent | link | π© |
1670 | Richest Customer Wealth | link | π© |
1678 | Goal Parser Interpretation | link | π© |
1684 | Count the Number of Consistent Strings | link | π© |
1704 | Determine if String Halves Are Alike | link | π© |
1720 | Decode XORed Array | link | π© |
1742 | Maximum Number of Balls in a Box | link | π© |
1768 | Merge Strings Alternately | link | π© |
1769 | Minimum Number of Operations to Move All Balls to Each Box | link | π¨ |
1773 | Count Items Matching a Rule | link | π© |
1796 | Second Largest Digit in a String | link | π© |
1805 | Number of Different Integers in a String | link | π© |
1812 | Determine Color of a Chessboard Square | link | π© |
1816 | Truncate Sentence | link | π© |
1817 | Finidng the Users Active Minutes | link | π¨ |
1827 | Minimum Operations to Make the Array Increasing | link | π© |
1828 | Queries on Number of Points Inside a Circle | link | π¨ |
1832 | Check if the Sentence Is Pangram | link | π© |
1844 | Replace All Digits with Characters | link | π© |
1854 | Maximum Population Year | link | π© |
1859 | Sorting the Sentence | link | π© |
1863 | Sum of All Subset XOR Totals | link | π© |
1876 | Substrings of Size Three with Distinct Characters | link | π© |
1877 | Minimize Maximum Pair Sum in Array | link | π¨ |
1880 | Check if Word Equals Summation of Two Words | link | π© |
1893 | Check if All the Integers in a Range Are Covered | link | π© |
1910 | Remove All Occurrences of a Substring | link | π¨ |
1913 | Maximum Product Difference Between Two Pairs | link | π© |
1920 | Build Array from Permutation | link | π© |
1929 | Concatenation of Array | link | π© |
1941 | Check if All Characters Have Equal Number of Occurrences | link | π© |
1967 | Number of Strings That Appear as Substrings in Word | link | π© |
2000 | Reverse Prefix of Word | link | π© |
2006 | Count Number of Pairs With Absolute Difference K | link | π© |
2011 | Final Value of Variable After Performing Operations | link | π© |
2037 | Minimum Number of Moves to Seat Everyone | link | π© |
2079 | Watering Plants | link | π¨ |
2085 | Count Common Words With One Occurrence | link | π© |
2103 | Rings and Rods | link | π© |
2114 | Maximum Number of Words Found in Sentences | link | π© |
2119 | A Number After a Double Reversal | link | π© |
2120 | Execution of All Suffix Instructions Staying in a Grid | link | π¨ |
2129 | Capitalize the Title | link | π© |
2149 | Rearrange Array Elements by Sign | link | π© |
2160 | Minimum Sum of Four Digit Number After Splitting Digits | link | π© |
2169 | Count Operations to Obtain Zero | link | π© |
2185 | Counting Words With a Given Prefix | link | π© |
2194 | Cells in a Range on an Excel Sheet | link | π© |
2215 | Find the Difference of Two Arrays | link | π© |
2221 | Find Triangular Sum of an Array | link | π¨ |
2283 | Check if Number Has Equal Digit Count and Digit Value | link | π© |
2309 | Greatest English Letter in Upper and Lower Case | link | π© |
2315 | Count Asterisks | link | π© |
2325 | Decode the Message | link | π© |
2373 | Largest Local Values in a Matrix | link | π© |
2390 | Removing Starts From a String | link | π¨ |
2391 | Minimum Amount of Time to Collect Garbage | link | π¨ |
2418 | Sort the People | link | π© |
2428 | Maximum Sum of an Hourglass | link | π¨ |
2433 | Find The Original Array of Prefix | link | π¨ |
2442 | Count Number of Distinct Integer After Reverse Operations | link | π¨ |
2446 | Determine if Two Events Have Conflict | link | π© |
2451 | Odd String Difference | link | π© |
2482 | Difference Between Ones and Zeros in Row and Column | link | π¨ |
2485 | Find the Pivot Integer | link | π© |
2496 | Maximum Value of a String in an Array | link | π© |
2506 | Count Pairs of Similar Strings | link | π© |
2540 | Minimum Common Value | link | π© |
2545 | Sort the Students by Their Kth Score | link | π¨ |
2553 | Separate the Digits in an Array | link | π© |
2559 | Count Vowel Strings in Ranges | link | π¨ |
2574 | Left and Right Sum Differences | link | π© |
2586 | Count the Number of Vowel Strings in Range | link | π© |
2595 | Number of Even and Odd Bits | link | π© |
2610 | Convert an Array Into a 2D Array With Conditions | link | π¨ |
2656 | Maximum Sum With Exactly K Elements | link | π© |
2696 | Minimum String Length After Removing Substrings | link | π© |
2716 | Minimize String Length | link | π© |
2744 | Find Maximum Number of String Pairs | link | π© |
2778 | Sum of Squares of Special Elements | link | π© |
2785 | Sort Vowels in a String | link | π¨ |
2788 | Split Strings by Separator | link | π© |
2798 | Number of Employees Who Met the Target | link | π© |
2810 | Faulty Keyboard | link | π© |
2824 | Count Pairs Whose Sum is Less than Target | link | π© |
2828 | Check if a String Is an Acronym of Words | link | π© |
2843 | Count Symmetric Integers | link | π© |
2848 | Points That Intersect With Cars | link | π© |
2859 | Sum of Values at Indices With K Set Bits | link | π© |
2864 | Maximum Odd Binary Number | link | π© |
2894 | Divisible and Non-divisible Sums Difference | link | π© |
2903 | Find Indices With Index and Value Difference I | link | π© |
2908 | Minimum Sum of Mountain Triplets I | link | π© |
2914 | Minimum Number of Changes to Make Binary String Beautiful | link | π¨ |
2917 | Find the K-or of an Array | link | π© |
2932 | Maximum Strong Pair XOR I | link | π© |
2942 | Find Words Containing Character | link | π© |
2960 | Count Tested Devices After Test Operations | link | π© |
2974 | Minimum Number Game | link | π© |
3005 | Count Elements With Maximum Frequency | link | π© |
3019 | Number of Changing Keys | link | π© |
3028 | Ant on the Boundary | link | π© |
3033 | Modify the Matrix | link | π© |
3046 | Split the Array | link | π© |
3065 | Minimum Operations to Exceed Threshold Value I | link | π© |
3074 | Apple Redistribution into Boxes | link | π© |
3095 | Shortest Subarray With OR at Least K I | link | π© |
3099 | Harshad Number | link | π© |
3110 | Score of String | link | π© |
3120 | Count the Number of Special Characters I | link | π© |
3136 | Valid Word | link | π© |
3151 | Special Array I | link | π© |
3158 | Find the XOR of Numbers Which Appear Twice | link | π© |
3162 | Find the Number of Good Pairs I | link | π© |
3163 | String Compression III | link | π¨ |
3168 | Minimum Number of Chairs in a Waiting Room | link | π© |
3174 | Clear Digits | link | π© |
3194 | Minimum Average of Smallest and Largest Elements | link | π© |
3195 | Find the Minimum Area to Cover All Ones I | link | π¨ |
3206 | Alternating Groups I | link | π© |
3210 | Find the Encrypted String | link | π© |
3216 | Lexicographically Smallest String After a Swap | link | π© |
3226 | Number of Bit Changes to Make Two Integers Equal | link | π© |
3232 | Find if Digit Game Can Be Won | link | π© |
3242 | Design Neighbor Sum Service | link | π© |
3248 | Snake in Matrix | link | π© |
3258 | Count Substrings That Satisfy K-Constraint I | link | π© |
3264 | Final Array State After K Multiplication Operations I | link | π© |
3271 | Hash Divided String | link | π¨ |
3289 | The Two Sneaky Numbers of Digitville | link | π© |
3280 | Convert Date to Binary | link | π© |
3285 | Find Indices of Stable Mountains | link | π© |
3270 | Find the Key of the Numbers | link | π© |
3274 | Check if Two Chessboard Squares Have the Same Color | link | π© |
3300 | Minimum Element After Replacement With Digit Sum | link | π© |
3304 | Find the K-th Character in String Game I | link | π© |
3324 | Find the Sequence of Strings Appeared on the Screen | link | π¨ |
3340 | Check Balanced String | link | π© |
3354 | Make Array Elements Equal to Zero | link | π© |
3360 | Stone Removal Game | link | π© |
3370 | Smallest Number With All Set Bits | link | π© |
3379 | Transformet Array | link | π© |
3386 | Button with Longest Push Time | link | π© |
3392 | Count Subarrays of Length Three With a Condition | link | π© |
3396 | Minimum Number of Operations to Make Elements in Array Distinct | link | π© |
3402 | Minimum Operations to Make Columns Strictly Increasing | link | π© |
3407 | Substring Matching Pattern | link | π© |
3412 | Find Mirror Score of a String | link | π¨ |
3417 | Zigzag Grid Traversal With Skip | link | π© |
3423 | Maximum Difference Between Adjacent Elements in a Circular Array | link | π© |
Run golangci-lint
command. There is a hidden configuration file for this tool inside root directory .golangci.yml
.
$ golangci-lint --color always run --verbose ./...
For every problem I have used test file to verify the solution with bunch of test cases.
You can run these tests with command:
$ go test -v ./...
Many solutions also contain benchmarks.
$ go test -bench=. -benchmem ./1---Two-Sum
goos: linux
goarch: amd64
cpu: 13th Gen Intel(solutions/R) Core(solutions/TM) i7-13700K
Benchmark_twoSum_Iterations-24 100000000 13.83 ns/op 16 B/op 1 allocs/op
Benchmark_twoSum_Map-24 43644519 25.72 ns/op 16 B/op 1 allocs/op
PASS