-
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
Showing
28 changed files
with
90 additions
and
5 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 |
---|---|---|
@@ -1,2 +1 @@ | ||
.DS_Store | ||
*.py | ||
.DS_Store |
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
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,34 @@ | ||
# 1071. Greatest Common Divisor of Strings | ||
# For two strings s and t, we say "t divides s" if and only if s = 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. | ||
|
||
# Example 1: | ||
|
||
# Input: str1 = "ABCABC", str2 = "ABC" | ||
# Output: "ABC" | ||
# Example 2: | ||
|
||
# Input: str1 = "ABABAB", str2 = "ABAB" | ||
# Output: "AB" | ||
# Example 3: | ||
|
||
# Input: str1 = "LEET", str2 = "CODE" | ||
# Output: "" | ||
|
||
|
||
# Constraints: | ||
|
||
# 1 <= str1.length, str2.length <= 1000 | ||
# str1 and str2 consist of English uppercase letters. | ||
|
||
from math import gcd | ||
|
||
class Solution: | ||
def gcdOfStrings(self, str1: str, str2: str) -> str: | ||
# base case that checks if keeping the strings create the same pattern | ||
if str1 + str2 != str2 + str1: | ||
return "" | ||
|
||
gcd_length = gcd(len(str1), len(str2)) | ||
return str1[:gcd_length] |
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,50 @@ | ||
# 1768. Merge Strings Alternately | ||
# 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. | ||
|
||
# Example 1: | ||
|
||
# Input: word1 = "abc", word2 = "pqr" | ||
# Output: "apbqcr" | ||
# Explanation: The merged string will be merged as so: | ||
# word1: a b c | ||
# word2: p q r | ||
# merged: a p b q c r | ||
# Example 2: | ||
|
||
# Input: word1 = "ab", word2 = "pqrs" | ||
# Output: "apbqrs" | ||
# Explanation: Notice that as word2 is longer, "rs" is appended to the end. | ||
# word1: a b | ||
# word2: p q r s | ||
# merged: a p b q r s | ||
# Example 3: | ||
|
||
# Input: word1 = "abcd", word2 = "pq" | ||
# Output: "apbqcd" | ||
# Explanation: Notice that as word1 is longer, "cd" is appended to the end. | ||
# word1: a b c d | ||
# word2: p q | ||
# merged: a p b q c d | ||
|
||
|
||
# Constraints: | ||
|
||
# 1 <= word1.length, word2.length <= 100 | ||
# word1 and word2 consist of lowercase English letters. | ||
|
||
|
||
class Solution: | ||
def mergeAlternately(self, word1: str, word2: str) -> str: | ||
output = "" | ||
|
||
count = 0 | ||
while len(word1) > count and len(word2) > count: | ||
output += word1[count] + word2[count] | ||
count += 1 | ||
|
||
output += word1[count:] | ||
output += word2[count:] | ||
|
||
return output |
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
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