Skip to content

Commit dd713b4

Browse files
committed
All DSA Problems
1 parent 5b4d579 commit dd713b4

File tree

5 files changed

+258
-1
lines changed

5 files changed

+258
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
*.o
88

99
# Ignore temporary files created by VS Code extensions
10-
tempCodeRunnerFile.cpp
10+
tempCodeRunnerFile.cpp
11+
12+
# Ignore build directories
13+
build/

Recursion/README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
2+
# C++ Data Structures & Algorithms (DSA) + LeetCode Solutions 🔥🔥🔥
3+
4+
![Language](https://img.shields.io/badge/Language-C%2B%2B-blue.svg)
5+
![License](https://img.shields.io/badge/License-MIT-green.svg)
6+
![Repo Status](https://img.shields.io/badge/Status-Active-brightgreen.svg)
7+
8+
Welcome to **C++ DSA & LeetCode Solutions**—your all-in-one resource for mastering data structures, algorithms and acing coding interviews!
9+
Welcome to **C++ DSA & LeetCode Solutions**—your all-in-one resource for mastering data structures, algorithms and acing coding interviews!
10+
This repository features clean, well-documented C++ implementations covering foundational CS concepts *and* a growing library of LeetCode problem solutions.
11+
12+
---
13+
14+
## 🚩 Features
15+
16+
- **Core DSA modules:** Binary search, sorting, arrays, and more.
17+
- **Extensive LeetCode practice:** Solutions mapped by problem number, ideal for interviews and self-study.
18+
- **Clear folder structure:** Easily find what you need, whether it’s theory or practice.
19+
- **Beginner-friendly:** Accessible explanations and simple build instructions.
20+
21+
---
22+
23+
## 📁 Repository Structure
24+
25+
DSA/
26+
├── Leetcode_Practice_Questions/ # LeetCode solutions
27+
│ ├── LC_1.cpp
28+
│ ├── LC_2.cpp
29+
│ └── README.md
30+
├── binarysearch.cpp # Core algorithms/data structures
31+
├── sort_0_&_1.cpp
32+
├── ...
33+
└── README.md # Start here!
34+
35+
```
36+
cppdsa/
37+
├── Leetcode_Practice_Questions/ # LeetCode solutions
38+
│ ├── LC_1.cpp
39+
│ ├── LC_2.cpp
40+
│ └── ...
41+
├── binarysearch.cpp # Core algorithms/data structures
42+
├── sort_0_&_1.cpp
43+
└── README.md # Start here!
44+
```
45+
46+
### Highlights
47+
48+
- **Leetcode_Practice_Questions**:
49+
- **Leetcode_Practice_Questions**:
50+
Organized, numbered solutions with a focused [README](./Leetcode_Practice_Questions/README.md).
51+
52+
- **Core DSA files**:
53+
Example:
54+
- `binarysearch.cpp`: Classic binary search implementation
55+
- `sort_0_&_1.cpp`: Fast 0/1 segregation in arrays
56+
- **Core DSA files**:
57+
Example:
58+
- `binarysearch.cpp`: Classic binary search implementation
59+
- `sort_0_&_1.cpp`: Fast 0/1 segregation in arrays
60+
- (More coming soon!)
61+
62+
---
63+
64+
## 🧑‍💻 Usage & Getting Started
65+
66+
1. **Clone the Repository**
67+
git clone https://github.com/alisamad1/DSA.git
68+
```bash
69+
git clone https://github.com/your-username/your-repo-name.git
70+
```
71+
2. **Enter the Directory**
72+
cd DSA
73+
```bash
74+
cd cppdsa
75+
```
76+
3. **Compile & Run Any File**
77+
- With `g++` (example for binarysearch):
78+
```
79+
g++ binarysearch.cpp -o binarysearch
80+
./binarysearch
81+
```
82+
```bash
83+
g++ binarysearch.cpp -o binarysearch
84+
./binarysearch
85+
```
86+
- Or open files using your favorite C++ IDE (e.g., VS Code, CLion).
87+
88+
---
89+
90+
## ✍️ Contributing
91+
92+
Contributions, bug reports, and suggestions are welcome!
93+
Whether you spot a typo, want to add a new algorithm, or have ideas for improvement, just open an issue or pull request.
94+
Contributions, bug reports, and suggestions are welcome!
95+
Whether you spot a typo, want to add a new algorithm, or have ideas for improvement, just open an issue or pull request on the new repository.
96+
97+
---
98+
99+
## 💡 Why Use This Repository?
100+
101+
- **Exam & Interview Prep:** Spot-on for students and job-seekers.
102+
- **Learning by Examples:** Study code, understand logic, and solve problems hands-on.
103+
- **Open Source:** Freely use and extend under the MIT License.
104+
105+
---
106+
107+
## 📫 Contact
108+
109+
Questions? Ideas?
110+
Questions? Ideas?
111+
Let’s connect!
112+
113+
- **LinkedIn:** https://www.linkedin.com/in/ali-samad-841b11301/
114+
- **Email:** aliussamad@gmail.com
115+
116+
---
117+
118+
## ⭐ Support
119+
120+
If this project helps you, please ⭐️ this repo and share it to support more learners!
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
#include <unordered_map>
5+
using namespace std;
6+
class Solution {
7+
vector<string> result;
8+
void solve(int idx, string &digits, string &temp, unordered_map<char, string> &mp) {
9+
if (idx >= digits.length()) {
10+
result.push_back(temp);
11+
return;
12+
}
13+
char ch = digits[idx];
14+
string str = mp[ch];
15+
for (int i = 0; i < str.length(); i++) {
16+
temp.push_back(str[i]);
17+
solve(idx + 1, digits, temp, mp);
18+
temp.pop_back();
19+
}
20+
}
21+
public:
22+
vector<string> letterCombinations(string digits) {
23+
result.clear();
24+
if (digits.length() == 0) {
25+
return {};
26+
}
27+
unordered_map<char, string> mp;
28+
mp['2'] = "abc";
29+
mp['3'] = "def";
30+
mp['4'] = "ghi";
31+
mp['5'] = "jkl";
32+
mp['6'] = "mno";
33+
mp['7'] = "pqrs";
34+
mp['8'] = "tuv";
35+
mp['9'] = "wxyz";
36+
string temp = "";
37+
solve(0, digits, temp, mp);
38+
return result;
39+
}
40+
};
41+
int main() {
42+
Solution sol;
43+
string digits;
44+
cout << "Enter digits: ";
45+
cin >> digits;
46+
vector<string> combos = sol.letterCombinations(digits);
47+
cout << "Letter combinations are:" << endl;
48+
for (const string &combo : combos) {
49+
cout << combo << endl;
50+
}
51+
return 0;
52+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
#include <algorithm>
5+
using namespace std;
6+
void solve(vector<string>& ans, string output, string str, int index) {
7+
if (index >= str.length()) {
8+
if (output.length() > 0) {
9+
ans.push_back(output);
10+
}
11+
return;
12+
}
13+
solve(ans, output, str, index + 1);
14+
solve(ans, output + str[index], str, index + 1);
15+
}
16+
vector<string> subsequences(string str) {
17+
vector<string> ans;
18+
string output = "";
19+
solve(ans, output, str, 0);
20+
sort(ans.begin(), ans.end());
21+
return ans;
22+
}
23+
int main() {
24+
string s;
25+
cout << "Enter a string: ";
26+
cin >> s;
27+
vector<string> result = subsequences(s);
28+
cout << "Subsequences are: " << endl;
29+
for (const string& sub : result) {
30+
cout << sub << endl;
31+
}
32+
return 0;
33+
}

Recursion/subsets.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
using namespace std;
5+
void createSubset(vector<int>& nums, int index, vector<vector<int>>& res, vector<int>& subset){
6+
if(index == nums.size()){
7+
res.push_back(subset);
8+
return;
9+
}
10+
subset.push_back(nums[index]);
11+
createSubset(nums,index+1, res, subset);
12+
subset.pop_back();
13+
createSubset(nums,index+1, res, subset);
14+
}
15+
vector<vector<int>> subsets(vector<int>& nums) {
16+
vector<vector<int>> res;
17+
vector<int> subset;
18+
createSubset(nums,0, res, subset);
19+
return res;
20+
}
21+
int main() {
22+
vector<int> nums = {1, 2, 3};
23+
vector<vector<int>> result = subsets(nums);
24+
cout << "All subsets:" << endl;
25+
for (const auto& s : result) {
26+
cout << "[";
27+
for (size_t i = 0; i < s.size(); ++i) {
28+
cout << s[i];
29+
if (i < s.size() - 1) {
30+
cout << ", ";
31+
}
32+
}
33+
cout << "]" << endl;
34+
}
35+
vector<int> nums2 = {0};
36+
vector<vector<int>> result2 = subsets(nums2);
37+
cout << "\nAll subsets for {0}:" << endl;
38+
for (const auto& s : result2) {
39+
cout << "[";
40+
for (size_t i = 0; i < s.size(); ++i) {
41+
cout << s[i];
42+
if (i < s.size() - 1) {
43+
cout << ", ";
44+
}
45+
}
46+
cout << "]" << endl;
47+
}
48+
return 0;
49+
}

0 commit comments

Comments
 (0)