From a9b1e0bdb340bb180e73034b3261648e932b818e Mon Sep 17 00:00:00 2001 From: Nishitha Daryn <100551304+nishithadaryn@users.noreply.github.com> Date: Thu, 8 May 2025 18:20:56 +0530 Subject: [PATCH 1/3] Create Duplicates checking cpp --- Arrays & Strings/Duplicates checking cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Arrays & Strings/Duplicates checking cpp diff --git a/Arrays & Strings/Duplicates checking cpp b/Arrays & Strings/Duplicates checking cpp new file mode 100644 index 0000000..bce13b2 --- /dev/null +++ b/Arrays & Strings/Duplicates checking cpp @@ -0,0 +1,13 @@ +class Solution { +public: + bool containsDuplicate(vector& 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; + } +}; + From c043d7a89e5adcb120b24f10dca3fff862d2caec Mon Sep 17 00:00:00 2001 From: Nishitha Daryn Date: Tue, 5 Aug 2025 11:21:19 +0530 Subject: [PATCH 2/3] Solution #14 -Nishitha - 05/08/2025 - commit #14 --- .../explanation.md | 14 ++++++++++++++ .../solution.cpp | 0 2 files changed, 14 insertions(+) create mode 100644 Problems/#14 - Longest Common Prefix - Easy/explanation.md create mode 100644 Problems/#14 - Longest Common Prefix - Easy/solution.cpp diff --git a/Problems/#14 - Longest Common Prefix - Easy/explanation.md b/Problems/#14 - Longest Common Prefix - Easy/explanation.md new file mode 100644 index 0000000..cb0f2b5 --- /dev/null +++ b/Problems/#14 - Longest Common Prefix - Easy/explanation.md @@ -0,0 +1,14 @@ +# Problem: Longest Common Prefix +## Language: C++ + +--- + +1. Start with the first string as the prefix. +2. For each string in the array: + - Reduce the prefix by comparing it with the current string. + - Stop early if prefix becomes empty. + +--- + + + diff --git a/Problems/#14 - Longest Common Prefix - Easy/solution.cpp b/Problems/#14 - Longest Common Prefix - Easy/solution.cpp new file mode 100644 index 0000000..e69de29 From e9e0e5c59d8ca5c92f76f527d4eff90783fbfbee Mon Sep 17 00:00:00 2001 From: Nishitha Daryn Date: Tue, 5 Aug 2025 16:35:20 +0530 Subject: [PATCH 3/3] Solution #14 - Nishitha - 05/08/2025 - commit #4 --- .../explanation.md | 20 +++++++++----- .../solution.cpp | 27 +++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/Problems/#14 - Longest Common Prefix - Easy/explanation.md b/Problems/#14 - Longest Common Prefix - Easy/explanation.md index cb0f2b5..cd3cf33 100644 --- a/Problems/#14 - Longest Common Prefix - Easy/explanation.md +++ b/Problems/#14 - Longest Common Prefix - Easy/explanation.md @@ -1,14 +1,22 @@ -# Problem: Longest Common Prefix -## Language: C++ +# 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**. --- -1. Start with the first string as the prefix. -2. For each string in the array: - - Reduce the prefix by comparing it with the current string. - - Stop early if prefix becomes empty. +## 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. --- + diff --git a/Problems/#14 - Longest Common Prefix - Easy/solution.cpp b/Problems/#14 - Longest Common Prefix - Easy/solution.cpp index e69de29..a6cff60 100644 --- a/Problems/#14 - Longest Common Prefix - Easy/solution.cpp +++ b/Problems/#14 - Longest Common Prefix - Easy/solution.cpp @@ -0,0 +1,27 @@ +// LeetCode #14 - Longest Common Prefix +// Language: C++ + +#include +#include + +using namespace std; + +class Solution { +public: + string longestCommonPrefix(vector& 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; + } +}; +