Skip to content

Commit 393e0aa

Browse files
authored
Added Two_Sum and Longest Palindromic Substring and Fixed Readme (codedecks-in#9)
* 1513 * Upload: "Flower Planting with No adjacent"-Python * Upload: codedecks-in#56 Merge Intervals (Python) * Updated Readme * Fixed Readme
1 parent 31df46e commit 393e0aa

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

Python/1_TwoSum.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#Difficulty = Easy
2+
#Submission Speed = 64.69%
3+
'''
4+
Solution-1:
5+
Example: nums = [3,2,1,4] target = 6
6+
Step1) Create a dictionary and populate it with elements of nums as the key and their corresponding index as values.
7+
8+
d = {
9+
3:0,
10+
2:1
11+
1:2
12+
4:3
13+
}
14+
Step2) Traverse the array and check for every element, if complement (target-element) exists in dictionary. Also check,
15+
the index of (target-element) is not same as that of element (using dictionary as we have saved index as values.)
16+
Traversing the array:
17+
# i=0 3,2,1,4
18+
^
19+
checking if (6-3) exists in dictionary?
20+
Yes it exists
21+
but d[6-3]==i (We are adding the same element to get the target which is invalid according to the question)
22+
23+
Step3) If we found a valid pair, then we return [i,d[complement]]
24+
'''
25+
'''
26+
Time Complexity = O(N)
27+
Space Complexity = O(N)
28+
'''
29+
30+
#Two-pass
31+
class Solution:
32+
def twoSum(self, nums: List[int], target: int) -> List[int]:
33+
d = {v:i for i,v in enumerate(nums)}
34+
for i in range(len(nums)):
35+
complement = target - nums[i]
36+
if complement in d and d[complement]!=i:
37+
return [i,d[complement]]
38+
39+
'''
40+
Solution-2: Single Pass:
41+
Instead of doing double passes (one pass while populating the dictionary and other while checking for complement),
42+
We do this in Single Pass. that is, checking for complement while populating the dictionary.
43+
Before inserting the element in dictionary, we check whether the complement already exists, if exists, we return both indexes
44+
(index of current element and index of its complement) and if not exists, we insert the element in dictionary.
45+
This way we wont have to check if the index of element and its complement is same or not.
46+
'''
47+
48+
#Single Pass
49+
class Solution:
50+
def twoSum(self, nums: List[int], target: int) -> List[int]:
51+
d = {}
52+
for i in range(len(nums)):
53+
complement = target - nums[i]
54+
if complement in d:
55+
return [d[complement],i]
56+
d[nums[i]]=i
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#Difficulty = Medium
2+
#Submission Speed = 48.40%
3+
'''
4+
Before Jumping onto Solution, know that:
5+
=>Palindromes are of two types: Odd Palindromes (length is odd) and Even Palindrome (length is even)
6+
=>We traverse the array, and for every element, we suppose that current element is the middle element and check for the largest palindromic
7+
substring that can be made which has middle element as the current element.
8+
Note that, In case of even palindrome, there are two middle elements.
9+
=> We check for both Odd and even Palindromes
10+
=> How to check for largest palindromic substring with given middle element?
11+
we maintain two pointers left and right.
12+
Initialize them with current index (middle element index) and In every loop we check s[left]==s[right] (and check for
13+
boundary errors too, left and right dont go out of the size of array or 0). In the body of the loop we decrease the left
14+
and increase the right pointer.
15+
'''
16+
'''
17+
Time Complexity = O(N^2)
18+
Space Complexity = O(N)
19+
'''
20+
21+
22+
class Solution:
23+
def longestPalindrome(self, s: str) -> str:
24+
longest = ""
25+
size = len(s)
26+
for i in range(size):
27+
#Odd Palindrome
28+
left,right = i,i
29+
ls = ""
30+
while 0<=left<size and 0<=right<size and s[left]==s[right]:
31+
if left==right:
32+
ls = s[left]
33+
else:
34+
ls = s[left] + ls + s[right]
35+
left-=1
36+
right+=1
37+
#Even Palindrome
38+
left,right = i,i+1
39+
rs = ""
40+
while 0<=left<size and 0<=right<size and s[left]==s[right]:
41+
rs = s[left] + rs + s[right]
42+
left-=1
43+
right+=1
44+
temp = ls if len(ls)>len(rs) else rs
45+
longest = temp if len(temp)>len(longest) else longest
46+
return longest

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@
127127
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
128128
| --- | ------------------------------------------------------------- | --------------------------------- | ------ | ------ | ---------- | --- | ------------- |
129129
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Java](./Java/valid-anagram.java) | _O(n)_ | _O(1)_ | Easy | | Unicode chars |
130+
|1| [Two Sum](https://leetcode.com/problems/two-sum/)| [Python](./Python/1_TwoSum.py)|_O(N)_|_O(N)_|Easy|||
131+
132+
<br/>
133+
<div align="right">
134+
<b><a href="#algorithms">⬆️ Back to Top</a></b>
135+
</div>
136+
<br/>
137+
138+
## Two Pointer
139+
140+
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
141+
| ---- | --------------------------------------------------------------------------------------------------- | ------------------------------------------------------- | -------- | --------- | ---------- | ----- | -------------- |
142+
|5|[Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)|[Python](./Python/5_LongestPalindromicSubstring.py)|_O(N^2)_|_O(N)_|Medium||Expand the Wings|
130143

131144
<br/>
132145
<div align="right">

0 commit comments

Comments
 (0)