Skip to content
Closed
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
56 changes: 56 additions & 0 deletions Python/1_TwoSum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#Difficulty = Easy
#Submission Speed = 64.69%
'''
Solution-1:
Example: nums = [3,2,1,4] target = 6
Step1) Create a dictionary and populate it with elements of nums as the key and their corresponding index as values.

d = {
3:0,
2:1
1:2
4:3
}
Step2) Traverse the array and check for every element, if complement (target-element) exists in dictionary. Also check,
the index of (target-element) is not same as that of element (using dictionary as we have saved index as values.)
Traversing the array:
# i=0 3,2,1,4
^
checking if (6-3) exists in dictionary?
Yes it exists
but d[6-3]==i (We are adding the same element to get the target which is invalid according to the question)

Step3) If we found a valid pair, then we return [i,d[complement]]
'''
'''
Time Complexity = O(N)
Space Complexity = O(N)
'''

#Two-pass
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
d = {v:i for i,v in enumerate(nums)}
for i in range(len(nums)):
complement = target - nums[i]
if complement in d and d[complement]!=i:
return [i,d[complement]]

'''
Solution-2: Single Pass:
Instead of doing double passes (one pass while populating the dictionary and other while checking for complement),
We do this in Single Pass. that is, checking for complement while populating the dictionary.
Before inserting the element in dictionary, we check whether the complement already exists, if exists, we return both indexes
(index of current element and index of its complement) and if not exists, we insert the element in dictionary.
This way we wont have to check if the index of element and its complement is same or not.
'''

#Single Pass
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
d = {}
for i in range(len(nums)):
complement = target - nums[i]
if complement in d:
return [d[complement],i]
d[nums[i]]=i
46 changes: 46 additions & 0 deletions Python/5_LongestPalindromicSubstring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#Difficulty = Medium
#Submission Speed = 48.40%
'''
Before Jumping onto Solution, know that:
=>Palindromes are of two types: Odd Palindromes (length is odd) and Even Palindrome (length is even)
=>We traverse the array, and for every element, we suppose that current element is the middle element and check for the largest palindromic
substring that can be made which has middle element as the current element.
Note that, In case of even palindrome, there are two middle elements.
=> We check for both Odd and even Palindromes
=> How to check for largest palindromic substring with given middle element?
we maintain two pointers left and right.
Initialize them with current index (middle element index) and In every loop we check s[left]==s[right] (and check for
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
and increase the right pointer.
'''
'''
Time Complexity = O(N^2)
Space Complexity = O(N)
'''


class Solution:
def longestPalindrome(self, s: str) -> str:
longest = ""
size = len(s)
for i in range(size):
#Odd Palindrome
left,right = i,i
ls = ""
while 0<=left<size and 0<=right<size and s[left]==s[right]:
if left==right:
ls = s[left]
else:
ls = s[left] + ls + s[right]
left-=1
right+=1
#Even Palindrome
left,right = i,i+1
rs = ""
while 0<=left<size and 0<=right<size and s[left]==s[right]:
rs = s[left] + rs + s[right]
left-=1
right+=1
temp = ls if len(ls)>len(rs) else rs
longest = temp if len(temp)>len(longest) else longest
return longest
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
| --- | ------------------------------------------------------------- | --------------------------------- | ------ | ------ | ---------- | --- | ------------- |
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Java](./Java/valid-anagram.java) | _O(n)_ | _O(1)_ | Easy | | Unicode chars |
|1| [Two Sum](https://leetcode.com/problems/two-sum/)| [Python](./Python/1_TwoSum.py)|_O(N)_|_O(N)_|Easy|||

<br/>
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
</div>
<br/>

## Two Pointer

| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
| ---- | --------------------------------------------------------------------------------------------------- | ------------------------------------------------------- | -------- | --------- | ---------- | ----- | -------------- |
|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|

<br/>
<div align="right">
Expand Down