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
daec629
commit 0179545
Showing
6 changed files
with
167 additions
and
0 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,14 @@ | ||
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2. | ||
|
||
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. | ||
|
||
word1 and word2 may be the same and they represent two individual words in the list. | ||
|
||
For example, | ||
Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. | ||
|
||
Given word1 = ��makes��, word2 = ��coding��, return 1. | ||
Given word1 = "makes", word2 = "makes", return 3. | ||
|
||
Note: | ||
You may assume word1 and word2 are both in the list. |
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,5 @@ | ||
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). | ||
|
||
Write a function to determine if a number is strobogrammatic. The number is represented as a string. | ||
|
||
For example, the numbers "69", "88", and "818" are all strobogrammatic. |
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,9 @@ | ||
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). | ||
|
||
Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high. | ||
|
||
For example, | ||
Given low = "50", high = "100", return 3. Because 69, 88, and 96 are three strobogrammatic numbers. | ||
|
||
Note: | ||
Because the range might be a large number, the low and high numbers are represented as 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,90 @@ | ||
class Solution(object): | ||
def strobogrammaticInRange(self, low, high): | ||
""" | ||
:type low: str | ||
:type high: str | ||
:rtype: int | ||
""" | ||
if int(low)>int(high):return 0 | ||
h = self.countLessThan(int(high)) | ||
l = self.countLessThan(int(low)) | ||
print l, h | ||
result = h-l | ||
if self.isStrobogrammatic(high): | ||
result+=1 | ||
return result | ||
|
||
def isStrobogrammatic(self, num): | ||
""" | ||
:type num: str | ||
:rtype: bool | ||
""" | ||
pairs = {"1":"1","0":"0", "8":"8", "6":"9","9":"6"} | ||
if len(num)%2==0: | ||
half = len(num)/2 | ||
else: | ||
half = len(num)/2+1 | ||
for i in range(half): | ||
if num[i] not in pairs: | ||
return False | ||
if pairs[num[i]]!=num[len(num)-1-i]: | ||
return False | ||
return True | ||
|
||
def strobolize(self, digits): | ||
pairs = {"1":"1","0":"0", "8":"8", "6":"9","9":"6"} | ||
return ''.join(reversed([pairs[d] for d in digits])) | ||
|
||
def countLessThan(self, n): | ||
strobos = [0, 1, 6, 8, 9] | ||
# first handle the edge case of single digits | ||
if n<=10: | ||
return len(filter(lambda x:x<n and x!=6 and x!=9, strobos)) | ||
|
||
total = 0 | ||
s = str(n) | ||
# all numbers with less digits than s are ok | ||
for i in range(1, len(s)): | ||
total += self.numbersWithDigits(i) | ||
print total | ||
|
||
# numbers with exactly len(s) digits | ||
if len(s)%2==0: | ||
half = len(s)/2 | ||
else: | ||
half = len(s)/2+1 | ||
l = len(s) | ||
for i in range(half): | ||
digit = int(s[i]) | ||
if i==0: | ||
first_candidates = len(filter(lambda x:x<digit and x!=0, strobos)) | ||
else: | ||
if len(s)%2 and i==half-1: | ||
first_candidates = len(filter(lambda x:x<digit, [0, 1, 8])) | ||
else: | ||
first_candidates = len(filter(lambda x:x<digit, strobos)) | ||
l-=2 | ||
total += first_candidates * self.sequencesWithDigits(l) | ||
if digit not in strobos: break | ||
if len(s)%2 and i==half-1 and digit not in [0,1,8]: break | ||
else: | ||
# whether the strobo number with same first half is less than n | ||
ss = s[:half] + self.strobolize(s[:half]) if len(s)%2==0 else s[:half] + self.strobolize(s[:half-1]) | ||
if int(ss)<n: | ||
total += 1 | ||
|
||
return total | ||
|
||
def numbersWithDigits(self, n): | ||
if n<2: | ||
return self.sequencesWithDigits(n) | ||
else: | ||
return 4*self.sequencesWithDigits(n-2) # prepend 1689 | ||
|
||
def sequencesWithDigits(self, n): | ||
if n<=0: | ||
return 1 | ||
elif n==1: | ||
return 3 # 0/1/8 | ||
else: | ||
return 5*self.sequencesWithDigits(n-2) # prepend 01689 |
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 @@ | ||
import string | ||
class Solution(object): | ||
def groupStrings(self, strings): | ||
""" | ||
:type strings: List[str] | ||
:rtype: List[List[str]] | ||
""" | ||
groups = [] | ||
alphabet = string.lowercase | ||
for s in strings: | ||
if not groups: | ||
groups.append([s]) | ||
continue | ||
found = False | ||
for group in groups: | ||
if len(s)!=len(group[0]): | ||
continue | ||
if len(s)==1: | ||
group.append(s) | ||
found = True | ||
break | ||
diff = (ord(s[0]) - ord(group[0][0]) +26)%26 | ||
for i in range(1, len(s)): | ||
if (ord(s[i]) - ord(group[0][i])+26)%26!= diff: | ||
break | ||
else: | ||
group.append(s) | ||
found = True | ||
break | ||
if not found: | ||
groups.append([s]) | ||
for group in groups: | ||
group.sort() | ||
return groups |
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,15 @@ | ||
Given a string, we can "shift" each of its letter to its successive letter, for example: "abc" -> "bcd". We can keep "shifting" which forms the sequence: | ||
|
||
"abc" -> "bcd" -> ... -> "xyz" | ||
Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence. | ||
|
||
For example, given: ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"], | ||
Return: | ||
|
||
[ | ||
["abc","bcd","xyz"], | ||
["az","ba"], | ||
["acef"], | ||
["a","z"] | ||
] | ||
Note: For the return value, each inner list's elements must follow the lexicographic order. |