diff --git a/README.md b/README.md index 234a096..83082bd 100644 --- a/README.md +++ b/README.md @@ -1,42 +1,130 @@ -# πŸ’‘ LeetCode Solutions Archive +# LeetCode Problems πŸš€ -Welcome to **LeetCode Solutions Repository**! πŸ‘¨β€πŸ’» -Here you'll find (almost) all the solutions to LeetCode problems, organized and documented for easy reference. +Welcome to the **LeetCode Problems** repository! This project is ideal for beginners who want to learn how to contribute to open-source. Here, you will find a collection of solutions to LeetCode problems in various programming languages. Dive in, explore, and start coding! -## About +[![Download Releases](https://img.shields.io/badge/Download%20Releases-Click%20Here-blue)](https://github.com/JuzJuz03/leetCodeProblems/releases) -- Covers **most problems** from LeetCode across all difficulty levels (Easy, Medium, Hard). -- Includes a wide range of **topics**: arrays, dynamic programming, trees, graphs, and more. -- Clean and readable **code** with comments and clear naming. -- Updated **regularly** with new problems and improvements. +## Table of Contents -## Purpose +- [Introduction](#introduction) +- [Getting Started](#getting-started) +- [Contributing](#contributing) +- [Languages Used](#languages-used) +- [Project Structure](#project-structure) +- [Topics Covered](#topics-covered) +- [Resources](#resources) +- [License](#license) -This repository serves multiple goals: -- Practice and improve problem-solving and coding skills. -- Build a personal reference of solved problems. -- Share solutions with others and encourage collaboration. +## Introduction -## πŸ™Œ Contribute +LeetCode is a platform for practicing coding skills through various challenges. This repository serves as a collection of solutions to some of those challenges. By participating, you can enhance your programming skills and learn how to work collaboratively on coding problems. -Found a bug, have a better approach, or want to add your own solution? -Feel free to open an issue or a pull request! -Together, we grow faster πŸš€ +## Getting Started -## πŸ”— Useful Links +To get started with this repository, you can follow these simple steps: + +1. **Clone the Repository**: Use the following command to clone the repository to your local machine: + ```bash + git clone https://github.com/JuzJuz03/leetCodeProblems.git + ``` + +2. **Explore the Solutions**: Navigate through the folders to find solutions in different programming languages. + +3. **Run the Code**: Each solution includes instructions on how to run the code. If you need to download specific files, visit the [Releases section](https://github.com/JuzJuz03/leetCodeProblems/releases) to get the latest updates. + +## Contributing + +We welcome contributions from everyone! Here’s how you can contribute: + +1. **Fork the Repository**: Click the fork button at the top right of this page. + +2. **Create a New Branch**: Use the following command to create a new branch: + ```bash + git checkout -b your-branch-name + ``` + +3. **Make Changes**: Implement your changes or add new solutions. + +4. **Commit Your Changes**: Use the following command to commit your changes: + ```bash + git commit -m "Add your message here" + ``` + +5. **Push to Your Branch**: Push your changes using: + ```bash + git push origin your-branch-name + ``` + +6. **Create a Pull Request**: Go to the original repository and click on "New Pull Request". + +By contributing, you help others learn and grow in their coding journey! + +## Languages Used + +This repository includes solutions in several programming languages: + +- **C** +- **C++** +- **Java** +- **Python** + +Feel free to add solutions in other languages as well! + +## Project Structure + +The repository is organized in a straightforward manner: + +``` +leetCodeProblems/ +β”‚ +β”œβ”€β”€ C/ +β”‚ β”œβ”€β”€ problem1.c +β”‚ └── problem2.c +β”‚ +β”œβ”€β”€ Cpp/ +β”‚ β”œβ”€β”€ problem1.cpp +β”‚ └── problem2.cpp +β”‚ +β”œβ”€β”€ Java/ +β”‚ β”œβ”€β”€ problem1.java +β”‚ └── problem2.java +β”‚ +└── Python/ + β”œβ”€β”€ problem1.py + └── problem2.py +``` + +Each folder contains solutions for specific problems, making it easy to navigate. + +## Topics Covered + +This repository covers a variety of topics related to algorithms and data structures, including but not limited to: + +- Arrays +- Strings +- Linked Lists +- Trees +- Graphs +- Dynamic Programming +- Backtracking + +Each solution aims to provide clear explanations and efficient code. + +## Resources + +Here are some helpful resources to aid your learning: + +- [LeetCode Official Website](https://leetcode.com/) +- [GeeksforGeeks](https://www.geeksforgeeks.org/) +- [HackerRank](https://www.hackerrank.com/) +- [Codecademy](https://www.codecademy.com/) + +These platforms offer a wealth of problems and tutorials to enhance your coding skills. + +## License + +This project is licensed under the MIT License. Feel free to use, modify, and distribute the code as you see fit. -- 🧩 [Visit LeetCode](https://leetcode.com/) -- πŸ™Œ [Contribute.mg](./CONTRIBUTING.md) --- -## Current solutions: -| Id | Title | Solution | Difficulty | -| -------- | ----------- | -------------- | ---------------- | -|1| [TwoSum](https://leetcode.com/problems/two-sum/)| [C](./Solutions/TwoSum/C/sol.c)|Easy| -|2|[AddTwoNumbers](https://leetcode.com/problems/add-two-numbers/)|[C](./Solutions/AddTwoNumbers/C/sol.c)|Medium| -|6|[ZigzagConversion](https://leetcode.com/problems/zigzag-conversion/)|[C](./Solutions/ZigzagConversion/sol.c)|Medium| -|9|[PalindromeNumber](https://leetcode.com/problems/palindrome-number/)|[C](./Solutions/PalindromeNumber/sol.c)|Easy| -|96|[UniqueBinarySearchTrees](https://leetcode.com/problems/unique-binary-search-trees/)|[C](./Solutions/UniqueBinarySearchTrees/C/sol.c)|Medium| -|1643|[KthSmallestInstructions](https://leetcode.com/problems/kth-smallest-instructions/)|[C](./Solutions/KthSmallestInstructions/C/sol.c)|Hard| - -Made with ❀️ by Marius +Thank you for visiting the **LeetCode Problems** repository! We hope you find it useful in your coding journey. For the latest updates, check the [Releases section](https://github.com/JuzJuz03/leetCodeProblems/releases) and stay tuned for more solutions! \ No newline at end of file diff --git a/Solutions/LongestCommonSubsequence.cpp/readme.md b/Solutions/LongestCommonSubsequence.cpp/readme.md new file mode 100644 index 0000000..034a7aa --- /dev/null +++ b/Solutions/LongestCommonSubsequence.cpp/readme.md @@ -0,0 +1,20 @@ +# Longest Common Subsequence + +## Idea of the Solution +We solve this problem using **recursion with memoization (top-down dynamic programming)**. +- We define `dp[i][j]` as the length of LCS of the first `i` characters of `text1` and the first `j` characters of `text2`. +- If the last characters match, we take `1 + dp[i-1][j-1]`. +- Otherwise, we take the maximum of `dp[i-1][j]` and `dp[i][j-1]`. + +## Time and Space Complexity +- **Time Complexity:** O(n * m), where n and m are the lengths of `text1` and `text2`. +- **Space Complexity:** O(n * m) for the memoization table. + +## Edge Cases Handled +- Either of the strings is empty β†’ returns 0. +- Strings with no common subsequence β†’ returns 0. + + +| Problem ID | Problem Title | Solution | Difficulty | +|------------|---------------|----------|------------| +| 1143 | [Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | [Java Solution](https://leetcode.com/problems/longest-common-subsequence/submissions/1792249047/) | Medium | diff --git a/Solutions/LongestCommonSubsequence.cpp/sol.java b/Solutions/LongestCommonSubsequence.cpp/sol.java new file mode 100644 index 0000000..aefe041 --- /dev/null +++ b/Solutions/LongestCommonSubsequence.cpp/sol.java @@ -0,0 +1,34 @@ +class Solution { + private int rec(int idx1, int idx2, String text1, String text2, int[][] dp) { + if (idx1 == 0 || idx2 == 0) { + return 0; + } + + if (dp[idx1][idx2] != -1) return dp[idx1][idx2]; + + if (text1.charAt(idx1 - 1) == text2.charAt(idx2 - 1)) { + dp[idx1][idx2] = 1 + rec(idx1 - 1, idx2 - 1, text1, text2, dp); + } else { + dp[idx1][idx2] = Math.max( + rec(idx1 - 1, idx2, text1, text2, dp), + rec(idx1, idx2 - 1, text1, text2, dp) + ); + } + + return dp[idx1][idx2]; + } + + public int longestCommonSubsequence(String text1, String text2) { + int n1 = text1.length(); + int n2 = text2.length(); + + int[][] dp = new int[n1 + 1][n2 + 1]; + for (int i = 0; i <= n1; i++) { + for (int j = 0; j <= n2; j++) { + dp[i][j] = -1; + } + } + + return rec(n1, n2, text1, text2, dp); + } +}