Skip to content

Solution #14 - Nishitha Daryn- 05/08/2025 #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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;
}
};