PR to add some problem by danghai · Pull Request #378 · keon/algorithms · GitHub
Skip to content
Merged
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
4 changes: 4 additions & 0 deletions README.md
4 changes: 4 additions & 0 deletions algorithms/strings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@
from .repeat_string import *
from .text_justification import *
from .min_distance import *
from .longest_common_prefix import *
from .rotate import *
from .first_unique_char import *
from .repeat_substring import *
27 changes: 27 additions & 0 deletions algorithms/strings/first_unique_char.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Given a string, find the first non-repeating character in it and return it's
index. If it doesn't exist, return -1.

For example:
s = "leetcode"
return 0.

s = "loveleetcode",
return 2.

Reference: https://leetcode.com/problems/first-unique-character-in-a-string/description/
"""
def first_unique_char(s):
"""
:type s: str
:rtype: int
"""
if (len(s) == 1):
return 0
ban = []
for i in range(len(s)):
if all(s[i] != s[k] for k in range(i + 1, len(s))) == True and s[i] not in ban:
return i
else:
ban.append(s[i])
return -1
18 changes: 17 additions & 1 deletion algorithms/strings/is_rotated.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,26 @@

accepts two strings
returns bool
Reference: https://leetcode.com/problems/rotate-string/description/
"""

def is_rotated(s1, s2):
if len(s1) == len(s2):
return s2 in s1 + s1
else:
return False
return False

"""
Another solution: brutal force
Complexity: O(N^2)
"""
def is_rotated_v1(s1, s2):
if len(s1) != len(s2):
return False
if len(s1) == 0:
return True

for c in range(len(s1)):
if all(s1[(c + i) % len(s1)] == s2[i] for i in range(len(s1))):
return True
return False
66 changes: 66 additions & 0 deletions algorithms/strings/longest_common_prefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:
Input: ["flower","flow","flight"]
Output: "fl"

Example 2:
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Reference: https://leetcode.com/problems/longest-common-prefix/description/
"""

"""
First solution: Horizontal scanning
"""
def common_prefix(s1, s2):
"Return prefix common of 2 strings"
if not s1 or not s2:
return ""
k = 0
while s1[k] == s2[k]:
k = k + 1
if k >= len(s1) or k >= len(s2):
return s1[0:k]
return s1[0:k]

def longest_common_prefix_v1(strs):
if not strs:
return ""
result = strs[0]
for i in range(len(strs)):
result = common_prefix(result, strs[i])
return result

"""
Second solution: Vertical scanning
"""
def longest_common_prefix_v2(strs):
if not strs:
return ""
for i in range(len(strs[0])):
for string in strs[1:]:
if i == len(string) or string[i] != strs[0][i]:
return strs[0][0:i]
return strs[0]

"""
Third solution: Divide and Conquer
"""
def longest_common_prefix_v3(strs):
if not strs:
return ""
return longest_common(strs, 0, len(strs) -1)

def longest_common(strs, left, right):
if left == right:
return strs[left]
mid = (left + right) // 2
lcp_left = longest_common(strs, left, mid)
lcp_right = longest_common(strs, mid + 1, right)
return common_prefix(lcp_left, lcp_right)
25 changes: 25 additions & 0 deletions algorithms/strings/repeat_substring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
Given a non-empty string check if it can be constructed by taking
a substring of it and appending multiple copies of the substring together.

For example:
Input: "abab"
Output: True
Explanation: It's the substring "ab" twice.

Input: "aba"
Output: False

Input: "abcabcabcabc"
Output: True
Explanation: It's the substring "abc" four times.

Reference: https://leetcode.com/problems/repeated-substring-pattern/description/
"""
def repeat_substring(s):
"""
:type s: str
:rtype: bool
"""
str = (s + s)[1:-1]
return s in str
18 changes: 18 additions & 0 deletions algorithms/strings/rotate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Given a strings s and int k, return a string that rotates k times

For example,
rotate("hello", 2) return "llohe"
rotate("hello", 5) return "hello"
rotate("hello", 6) return "elloh"
rotate("hello", 7) return "llohe"

accepts two strings
returns bool
"""
def rotate(s, k):
double_s = s + s
if k <= len(s):
return double_s[k:k + len(s)]
else:
return double_s[k-len(s):k]
46 changes: 44 additions & 2 deletions tests/test_strings.py