Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Arrays & Strings/Duplicates checking cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
sort(nums.begin(), nums.end()); // O(n log n)
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] == nums[i - 1]) {
return true;
}
}
return false;
}
};

22 changes: 22 additions & 0 deletions Problems/#14 - Longest Common Prefix - Easy/explanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Leetcode Problem #14: Longest Common Prefix (Easy)

## Problem

Given an array of strings `strs`, return the **longest common prefix** string amongst them.
If there is no common prefix, return an **empty string**.

---

## Approach

- Start with the first string as the **initial prefix**.
- Compare it with the next string:
- If the current string doesn't start with the prefix, **trim the prefix** by one character from the end.
- Repeat until it matches or becomes an empty string.
- Continue this for all strings in the list.

---




27 changes: 27 additions & 0 deletions Problems/#14 - Longest Common Prefix - Easy/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// LeetCode #14 - Longest Common Prefix
// Language: C++

#include <string>
#include <vector>

using namespace std;

class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.empty()) return "";

string prefix = strs[0];

for (int i = 1; i < strs.size(); ++i) {
int j = 0;
while (j < prefix.size() && j < strs[i].size() && prefix[j] == strs[i][j])
++j;
prefix = prefix.substr(0, j);
if (prefix.empty()) return "";
}

return prefix;
}
};