Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0720330
adding award
iamAntimPal Apr 14, 2025
68262fe
Update README.md
iamAntimPal Apr 14, 2025
a45ca74
Update README.md
iamAntimPal Apr 14, 2025
979ef96
Update README.md
iamAntimPal Apr 14, 2025
61abd73
Update README.md
iamAntimPal Apr 14, 2025
2be2b7a
Update README.md
iamAntimPal Apr 14, 2025
7f23574
Update README.md
iamAntimPal Apr 14, 2025
43cf756
asd
iamAntimPal Apr 14, 2025
181c95e
Update readme.md
iamAntimPal Apr 14, 2025
a9ac999
add
iamAntimPal Apr 14, 2025
321930a
Update 1071. Greatest Common Divisor of Strings.py
iamAntimPal Apr 14, 2025
7ddcc74
Create readme.md
iamAntimPal Apr 14, 2025
22e6ff3
Update readme.md
iamAntimPal Apr 14, 2025
9f94a0b
Create 1431. Kids With the Greatest Number of Candies.py
iamAntimPal Apr 14, 2025
2ebd5f8
Update 1431. Kids With the Greatest Number of Candies.py
iamAntimPal Apr 14, 2025
6612aeb
Create readme.md
iamAntimPal Apr 14, 2025
0700a1f
Create 605. Can Place Flowers.py
iamAntimPal Apr 14, 2025
4253c68
Update 605. Can Place Flowers.py
iamAntimPal Apr 14, 2025
5511c3a
Create readme.md
iamAntimPal Apr 14, 2025
b96bedb
Create 2300. Successful Pairs of Spells and Potions.py
iamAntimPal Apr 14, 2025
bf24627
Update 2300. Successful Pairs of Spells and Potions.py
iamAntimPal Apr 14, 2025
3b118f5
Create readme.md
iamAntimPal Apr 14, 2025
f7d51df
Update readme.md
iamAntimPal Apr 14, 2025
09e36d2
Create 162. Find Peak Element.py
iamAntimPal Apr 14, 2025
b4aad28
Update 162. Find Peak Element.py
iamAntimPal Apr 14, 2025
255117c
Create readme.md
iamAntimPal Apr 14, 2025
9806792
Update readme.md
iamAntimPal Apr 14, 2025
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# LeetCode Python Solutions

![LeetCode Logo](https://upload.wikimedia.org/wikipedia/commons/1/19/LeetCode_logo_black.png)
<!-- <p align="center"><img src="https://upload.wikimedia.org/wikipedia/commons/1/19/LeetCode_logo_black.png"></p> -->

<p align="center"><img src="./assets/LeetCode_75.gif"></p>


This repository contains Python solutions for various LeetCode problems. Each problem is organized in its own directory under the `Solution/` folder, with a `readme.md` file providing the problem description, examples, constraints, and solutions in multiple programming languages.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution(object):
def gcdOfStrings(self, str1, str2):
"""
:type str1: str
:type str2: str
:rtype: str
"""
if str1 + str2 != str2 + str1:
return ""

def gcd(a, b):
while b:
a, b = b, a % b
return a

return str1[:gcd(len(str1), len(str2))]
118 changes: 118 additions & 0 deletions Solution/1071. Greatest Common Divisor of Strings/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@


<!-- problem:start -->

# [1071. Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings)

---
- **comments**: true
- **difficulty**: Easy
- **rating**: 1397
- **source**: Weekly Contest 139 Q1
- **tags**:
- Math
- String
---

## Description

<!-- description:start -->

<p>For two strings <code>s</code> and <code>t</code>, we say &quot;<code>t</code> divides <code>s</code>&quot; if and only if <code>s = t + t + t + ... + t + t</code> (i.e., <code>t</code> is concatenated with itself one or more times).</p>

<p>Given two strings <code>str1</code> and <code>str2</code>, return <em>the largest string </em><code>x</code><em> such that </em><code>x</code><em> divides both </em><code>str1</code><em> and </em><code>str2</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> str1 = &quot;ABCABC&quot;, str2 = &quot;ABC&quot;
<strong>Output:</strong> &quot;ABC&quot;
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> str1 = &quot;ABABAB&quot;, str2 = &quot;ABAB&quot;
<strong>Output:</strong> &quot;AB&quot;
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> str1 = &quot;LEET&quot;, str2 = &quot;CODE&quot;
<strong>Output:</strong> &quot;&quot;
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= str1.length, str2.length &lt;= 1000</code></li>
<li><code>str1</code> and <code>str2</code> consist of English uppercase letters.</li>
</ul>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1

<!-- tabs:start -->

#### Python3

```python
class Solution:
def gcdOfStrings(self, str1: str, str2: str) -> str:
def check(a, b):
c = ""
while len(c) < len(b):
c += a
return c == b

for i in range(min(len(str1), len(str2)), 0, -1):
t = str1[:i]
if check(t, str1) and check(t, str2):
return t
return ''
```

#### Java

```java
class Solution {
public String gcdOfStrings(String str1, String str2) {
if (!(str1 + str2).equals(str2 + str1)) {
return "";
}
int len = gcd(str1.length(), str2.length());
return str1.substring(0, len);
}

private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
```

#### C++

```cpp
class Solution {
public:
string gcdOfStrings(string str1, string str2) {
if (str1 + str2 != str2 + str1) return "";
int n = __gcd(str1.size(), str2.size());
return str1.substr(0, n);
}
};
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import List

class Solution:
def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
# Find the maximum number of candies that any child currently has
max_candies = max(candies)

# Create a list of boolean values, where each value indicates whether a child
# can have the greatest number of candies by adding the extraCandies to their current amount
can_have_most_candies = [
(child_candies + extraCandies) >= max_candies for child_candies in candies
]

# Return the list of boolean values
return can_have_most_candies

123 changes: 123 additions & 0 deletions Solution/1431. Kids With the Greatest Number of Candies/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@


<!-- problem:start -->

# [1431. Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies)

---
- **comments**: true
- **difficulty**: Easy
- **rating**: 1176
- **source**: Biweekly Contest 25 Q1
- **tags**:
- Array
---

## Description

<!-- description:start -->

<p>There are <code>n</code> kids with candies. You are given an integer array <code>candies</code>, where each <code>candies[i]</code> represents the number of candies the <code>i<sup>th</sup></code> kid has, and an integer <code>extraCandies</code>, denoting the number of extra candies that you have.</p>

<p>Return <em>a boolean array </em><code>result</code><em> of length </em><code>n</code><em>, where </em><code>result[i]</code><em> is </em><code>true</code><em> if, after giving the </em><code>i<sup>th</sup></code><em> kid all the </em><code>extraCandies</code><em>, they will have the <strong>greatest</strong> number of candies among all the kids</em><em>, or </em><code>false</code><em> otherwise</em>.</p>

<p>Note that <strong>multiple</strong> kids can have the <strong>greatest</strong> number of candies.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> candies = [2,3,5,1,3], extraCandies = 3
<strong>Output:</strong> [true,true,true,false,true]
<strong>Explanation:</strong> If you give all extraCandies to:
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> candies = [4,2,1,1,2], extraCandies = 1
<strong>Output:</strong> [true,false,false,false,false]
<strong>Explanation:</strong> There is only 1 extra candy.
Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> candies = [12,1,12], extraCandies = 10
<strong>Output:</strong> [true,false,true]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>n == candies.length</code></li>
<li><code>2 &lt;= n &lt;= 100</code></li>
<li><code>1 &lt;= candies[i] &lt;= 100</code></li>
<li><code>1 &lt;= extraCandies &lt;= 50</code></li>
</ul>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1

<!-- tabs:start -->

#### Python3

```python
class Solution:
def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
mx = max(candies)
return [candy + extraCandies >= mx for candy in candies]
```

#### Java

```java
class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int mx = 0;
for (int candy : candies) {
mx = Math.max(mx, candy);
}
List<Boolean> res = new ArrayList<>();
for (int candy : candies) {
res.add(candy + extraCandies >= mx);
}
return res;
}
}
```

#### C++

```cpp
class Solution {
public:
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
int mx = *max_element(candies.begin(), candies.end());
vector<bool> res;
for (int candy : candies) {
res.push_back(candy + extraCandies >= mx);
}
return res;
}
};
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
10 changes: 10 additions & 0 deletions Solution/162. Find Peak Element/162. Find Peak Element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def findPeakElement(self, nums: List[int]) -> int:
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) >> 1
if nums[mid] > nums[mid + 1]:
right = mid
else:
left = mid + 1
return left
Loading