-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c86e05
commit f7e7ea0
Showing
20 changed files
with
224 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"python.testing.unittestArgs": [ | ||
"-v", | ||
"-s", | ||
".", | ||
"-p", | ||
"test_*.py" | ||
], | ||
"python.testing.pytestEnabled": false, | ||
"python.testing.unittestEnabled": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## String | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
## Binary Tree | ||
|
||
### Tree height problem | ||
|
||
**Statement**: Given a binary tree, find its height. | ||
|
||
**Approach**: Transverse the tree, keeping track of the height of the left and right subtrees. Return the maximum height. | ||
1. Base case: empty (or null) tree has height -1. | ||
2. Recursive case: return 1 + the maximum height of the left and right subtrees. | ||
|
||
|
||
```python | ||
def height(node): | ||
|
||
# empty tree | ||
if node is None: | ||
return -1 | ||
|
||
# recursive case | ||
return 1 + max(height(node.left), height(node.right)) | ||
``` | ||
|
||
**Complexity**: | ||
- Time: O($n$), need to transverse every node on the tree. | ||
- Space: O($n$), we have n calls to ```height()```. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Binary file not shown.
Binary file added
BIN
+1000 Bytes
string_array/__pycache__/greatest_common_divisor_of_strings.cpython-310.pyc
Binary file not shown.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" | ||
1071. Greatest Common Divisor of Strings | ||
Easy | ||
For two strings s and t, we say "t divides s" if and only | ||
if s = t + t + t + ... + t + t (i.e., t is concatenated with | ||
itself one or more times). | ||
Given two strings str1 and str2, return the largest string x | ||
such that x divides both str1 and str2. | ||
""" | ||
|
||
def gcdOfStrings(str1, str2): | ||
m = len(str1) | ||
n = len(str2) | ||
|
||
if m>=n: | ||
divisor = str2 | ||
word = str1 | ||
else: | ||
divisor = str1 | ||
word = str2 | ||
|
||
static_divisor = divisor | ||
cur = len(divisor) | ||
while cur > 0: | ||
remainder = len(word) % len(divisor) | ||
if remainder==0: | ||
k = int((len(word)/len(divisor))) | ||
divisable = word == divisor * k | ||
if divisable: | ||
if len(static_divisor) % len(divisor) == 0: | ||
return divisor | ||
|
||
cur-=1 | ||
divisor = divisor[:cur] | ||
return "" | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
print(gcdOfStrings("ABCABC", "ABC")) # -> "ABC" | ||
print(gcdOfStrings("ABABAB", "AB")) # -> "AB" |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
1768. Merge Strings Alternately | ||
Easy | ||
You are given two strings word1 and word2. | ||
Merge the strings by adding letters in alternating order, | ||
starting with word1. If a string is longer than the other, | ||
append the additional letters onto the end of the merged string. | ||
Return the merged string. | ||
""" | ||
|
||
def mergeAlternatively(word1, word2): | ||
cur1 = 0 | ||
cur2 = 0 | ||
ans = "" | ||
|
||
while cur1 < len(word1) and cur2 < len(word2): | ||
ans += word1[cur1] + word2[cur2] | ||
|
||
cur1+=1 | ||
cur2+=1 | ||
|
||
if cur1 == len(word1): | ||
if cur2 < len(word2): | ||
ans+=word2[cur2:] | ||
return ans | ||
|
||
if cur2 == len(word2): | ||
if cur1 < len(word1): | ||
ans+=word1[cur1:] | ||
return ans | ||
return ans | ||
|
||
|
||
|
||
print(mergeAlternatively("abc","pqr")) #-> "apbqcr" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import sys |
Binary file not shown.
Binary file added
BIN
+1.57 KB
string_array/tests/__pycache__/test_greatest_common_divisor_of_strings.cpython-310.pyc
Binary file not shown.
41 changes: 41 additions & 0 deletions
41
string_array/tests/test_greatest_common_divisor_of_strings.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import unittest as ut | ||
from string_array.greatest_common_divisor_of_strings import gcdOfStrings | ||
|
||
|
||
class TestGCDOfStrings(ut.TestCase): | ||
def test_common_divisor_exists(self): | ||
str1 = "ABCABC" | ||
str2 = "ABC" | ||
expected_output = "ABC" | ||
self.assertEqual(gcdOfStrings(str1, str2), expected_output) | ||
|
||
def test_no_common_divisor(self): | ||
str1 = "ABABAB" | ||
str2 = "CD" | ||
expected_output = "" | ||
self.assertEqual(gcdOfStrings(str1, str2), expected_output) | ||
|
||
def test_empty_strings(self): | ||
str1 = "" | ||
str2 = "" | ||
expected_output = "" | ||
self.assertEqual(gcdOfStrings(str1, str2), expected_output) | ||
|
||
def test_same_string(self): | ||
str1 = "XYZ" | ||
str2 = "XYZ" | ||
expected_output = "XYZ" | ||
self.assertEqual(gcdOfStrings(str1, str2), expected_output) | ||
|
||
def test_long_string(self): | ||
str1 = "TAUXX" * 5 | ||
str2 = "TAUXX" * 9 | ||
expected_output = "TAUXX" | ||
self.assertEqual(gcdOfStrings(str1, str2), expected_output) | ||
|
||
|
||
def test_more_strings(self): | ||
str1 = "AAAAAAAAA" | ||
str2 = "AAACCC" | ||
expected_output = "" | ||
self.assertEqual(gcdOfStrings(str1, str2), expected_output) |
File renamed without changes.
File renamed without changes.