Skip to content

jobearrr/LeetSwift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧮 LeetSwift

Solutions to LeetCode problems written in Swift.

GitHub last commit CI Status Maintainability Test Coverage MIT License LinkedIn

Requirements

  • Xcode 15.2+
  • Swift 5.9

Running the solutions

Each solution has test cases that can be found in the SolutionsTests folder.
I try to write tests covering as much of the code as possible (on top of the test cases provided by LeetCode).
You can modify the test cases, and provide new inputs and expected outputs.

Project structure

Here's the representation of the project structure, to make it easier to navigate through all the content.

Please pay attention to this if adding problems and implementing solutions, so everything conforms to this folder structure.

.
├── LeetSwift                                      # Companion app
│   └── ...                                        # Project files
├── LeetSwift.xcworkspace
├── Package.swift                                  # LeetSwift package spec
├── Sources
│   ├── Core                                       # Data structures, helpers, etc.
│   │   └── ...
│   ├── Solutions
│   │   ├── ...
│   │   └── XXX - Problem title
│   │       ├── ProblemXXXDefinition.swift         # Problem definition
│   │       ├── ProblemXXX<M>Solution.swift        # Solution M
│   │       ├── ProblemXXX<N>Solution.swift        # Solution N
│   │       ├── ...                                # Other solutions
│   │       └── README.md                          # Documentation and solutions
│   └── TestSupport                                # Supporting classes / extensions
│       ├── Array+ListNode.swift
│       ├── TestCaseProviding.swift
│       └── TestData.swift
└── Tests
    ├── CoreTests
    │   └── ...
    ├── SolutionsTests
    │   ├── ProblemXXXTests.swift                  # Test all solutions for Problem XXX
    │   └── ...                                    # Other tests
    └── TestSupportTests
        └── Utils
            └── ...                                # Test support classes

Problems

Below, you can find a table with all the problems with their corresponding solution. You can find a detailed walkthrough of the solutions to these problems on my blog jobear.dev.

# Problem & Solutions Topics Difficulty
1 Two Sum A HT Difficulty
2 Add Two Numbers LL M R Difficulty
3 Longest Substring Without Repeating Characters HT S SW Difficulty
4 Median of Two Sorted Arrays A BS DC Difficulty
5 Longest Palindromic Substring TP S DP Difficulty
6 Zigzag Conversion S Difficulty
7 Reverse Integer M Difficulty
8 String to Integer (atoi) S Difficulty
9 Palindrome Number M Difficulty
10 Regular Expression Matching S DP R Difficulty
11 Container With Most Water A TP G Difficulty
12 Integer to Roman HT M S Difficulty
13 Roman to Integer HT M S Difficulty
14 Longest Common Prefix S T Difficulty
15 3Sum A TP SO Difficulty
16 3Sum Closest A TP SO Difficulty
17 Letter Combinations of a Phone Number HT S BT Difficulty
18 4Sum A TP SO Difficulty
19 Remove Nth Node From End of List LL TP Difficulty
20 Valid Parentheses S ST Difficulty
21 Merge Two Sorted Lists LL R Difficulty
22 Generate Parentheses S DP BT Difficulty
23 Merge k Sorted Lists LL DC H MS Difficulty
24 Swap Nodes in Pairs LL R Difficulty
25 Reverse Nodes in k-Group LL R Difficulty
26 Remove Duplicates from Sorted Array A TP Difficulty
27 Remove Element A TP Difficulty
28 Find the Index of the First Occurrence in a String TP S SM Difficulty
29 Divide Two Integers B BM Difficulty
30 Substring with Concatenation of All Words HT S SW Difficulty
31 Next Permutation A TP Difficulty
32 Longest Valid Parentheses S DP ST Difficulty
33 Search in Rotated Sorted Array Difficulty
34 Find First and Last Position of Element in Sorted Array Difficulty
35 Search Insert Position Difficulty
36 Valid Sudoku Difficulty
37 Sudoku Solver Difficulty
38 Count and Say Difficulty
39 Combination Sum Difficulty
40 Combination Sum II Difficulty
41 First Missing Positive Difficulty
42 Trapping Rain Water Difficulty
43 Multiply Strings Difficulty
44 Wildcard Matching Difficulty
45 Jump Game II Difficulty
46 Permutations Difficulty
47 Permutations II Difficulty
48 Rotate Image Difficulty
49 Group Anagrams Difficulty
50 Pow(x, n) Difficulty
51 N-Queens Difficulty
52 N-Queens II Difficulty
53 Maximum Subarray Difficulty
54 Spiral Matrix Difficulty
55 Jump Game Difficulty
56 Merge Intervals Difficulty
57 Insert Interval Difficulty
58 Length of Last Word Difficulty
59 Spiral Matrix II Difficulty
60 Permutation Sequence Difficulty
61 Rotate List Difficulty
62 Unique Paths Difficulty
63 Unique Paths II Difficulty
64 Minimum Path Sum Difficulty
65 Valid Number Difficulty
66 Plus One Difficulty
67 Add Binary Difficulty
68 Text Justification Difficulty
69 Sqrt(x) Difficulty
70 Climbing Stairs Difficulty
71 Simplify Path Difficulty
72 Edit Distance Difficulty
73 Set Matrix Zeroes Difficulty
74 Search a 2D Matrix Difficulty
75 Sort Colors Difficulty
76 Minimum Window Substring Difficulty
77 Combinations Difficulty
78 Subsets Difficulty
79 Word Search Difficulty
80 Remove Duplicates from Sorted Array II Difficulty
81 Search in Rotated Sorted Array II Difficulty
82 Remove Duplicates from Sorted List II Difficulty
83 Remove Duplicates from Sorted List Difficulty
84 Largest Rectangle in Histogram Difficulty
85 Maximal Rectangle Difficulty
86 Partition List Difficulty
87 Scramble String Difficulty
88 Merge Sorted Array Difficulty
89 Gray Code Difficulty
90 Subsets II Difficulty
91 Decode Ways Difficulty
92 Reverse Linked List II Difficulty
93 Restore IP Addresses Difficulty
94 Binary Tree Inorder Traversal Difficulty
95 Unique Binary Search Trees II Difficulty
96 Unique Binary Search Trees Difficulty
97 Interleaving String Difficulty
98 Validate Binary Search Tree Difficulty
99 Recover Binary Search Tree Difficulty
100 Same Tree Difficulty

Topics

  • A : Array
  • HT : Hash Table
  • LL: Linked List
  • M: Math
  • R: Recursion
  • S: String
  • SW: Sliding Window
  • BS: Binary Search
  • DC: Divide and Conquer
  • TP: Two Pointers
  • DP: Dynamic Programming
  • G: Greedy
  • T: Trie
  • SO: Sorting
  • BT: Backtracking
  • ST: Stack
  • H: Heap
  • MS: Merge Sort
  • SM: String Matching
  • BM: Bit Manipulation

TDD Approach

Here, I use the TDD approach, writing the test cases before writing the solution. This way, I can quickly verify if the solution works as expected.

The goal is always to have enough test cases for all the problems and then work on the solution until all of these test cases are green.

If you would like to contribute, please use this same approach.

License

Please note this project uses the GNU AGPLv3 license, which requires any code or software that uses it to make their code completely available.

This project is intended to be used for educational purposes only.

Please acknowledge the license and contact me if you intend to use any content from here.

About

Solutions for LeetCode problems, written in Swift

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages