Skip to content

Commit 7520795

Browse files
Hai Hoang Danggoswami-rahul
authored andcommitted
Added some solution (keon#362)
* Add simplify path * Add repeat string
1 parent 447e02d commit 7520795

File tree

7 files changed

+72
-2
lines changed

7 files changed

+72
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ If you want to uninstall algorithms, it is as simple as:
291291
- [caesar_cipher](algorithms/strings/caesar_cipher.py)
292292
- [contain_string](algorithms/strings/contain_string.py)
293293
- [count_binary_substring](algorithms/strings/count_binary_substring.py)
294+
- [repeat_string](algorithms/strings/repeat_string.py)
294295
- [tree](algorithms/tree)
295296
- [bst](algorithms/tree/tree/bst)
296297
- [array2bst](algorithms/tree/bst/array2bst.py)
@@ -343,6 +344,7 @@ If you want to uninstall algorithms, it is as simple as:
343344
- [join_with_slash](algorithms/unix/path/join_with_slash.py)
344345
- [full_path](algorithms/unix/path/full_path.py)
345346
- [split](algorithms/unix/path/split.py)
347+
- [simplify_path](algorithms/unix/path/simplify_path.py)
346348
- [union-find](algorithms/union-find)
347349
- [count_islands](algorithms/union-find/count_islands.py)
348350

algorithms/strings/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@
2727
from .caesar_cipher import *
2828
from .contain_string import *
2929
from .count_binary_substring import *
30+
from .repeat_string import *
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1.
3+
4+
For example, with A = "abcd" and B = "cdabcdab".
5+
6+
Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of it; and B is not a substring of A repeated two times ("abcdabcd").
7+
8+
Note:
9+
The length of A and B will be between 1 and 10000.
10+
11+
Reference: https://leetcode.com/problems/repeated-string-match/description/
12+
"""
13+
def repeat_string(A, B):
14+
count = 1
15+
tmp = A
16+
max_count = (len(B) / len(A)) + 1
17+
while not(B in tmp):
18+
tmp = tmp + A
19+
if (count > max_count):
20+
count = -1
21+
break
22+
count = count + 1
23+
24+
return count

algorithms/unix/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .path.join_with_slash import *
22
from .path.full_path import *
33
from .path.split import *
4+
from .path.simplify_path import *
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Given an absolute path for a file (Unix-style), simplify it.
3+
4+
For example,
5+
path = "/home/", => "/home"
6+
path = "/a/./b/../../c/", => "/c"
7+
8+
Corner Cases:
9+
10+
Did you consider the case where path = "/../"?
11+
In this case, you should return "/".
12+
Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
13+
In this case, you should ignore redundant slashes and return "/home/foo".
14+
15+
Reference: https://leetcode.com/problems/simplify-path/description/
16+
"""
17+
18+
import os
19+
def simplify_path_v1(path):
20+
return os.path.abspath(path)
21+
22+
def simplify_path_v2(path):
23+
stack, tokens = [], path.split("/")
24+
for token in tokens:
25+
if token == ".." and stack:
26+
stack.pop()
27+
elif token != ".." and token != "." and token:
28+
stack.append(token)
29+
return "/" + "/".join(stack)

tests/test_strings.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
strong_password,
3030
caesar_cipher,
3131
contain_string,
32-
count_binary_substring
32+
count_binary_substring,
33+
repeat_string
3334
)
3435

3536
import unittest
@@ -421,5 +422,10 @@ def test_count_binary_substring(self):
421422
self.assertEqual(4, count_binary_substring("10101"))
422423
self.assertEqual(3, count_binary_substring("00110"))
423424

425+
class TestCountBinarySubstring(unittest.TestCase):
426+
def test_repeat_string(self):
427+
self.assertEqual(3, repeat_string("abcd","cdabcdab"))
428+
self.assertEqual(4, repeat_string("bb", "bbbbbbb"))
429+
424430
if __name__ == "__main__":
425431
unittest.main()

tests/test_unix.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from algorithms.unix import (
22
join_with_slash,
33
full_path,
4-
split
4+
split,
5+
simplify_path_v1, simplify_path_v2
56
)
67
import os
78
import unittest
@@ -33,3 +34,9 @@ def test_split(self):
3334
expect_result = split(path)
3435
self.assertEqual("algorithms/unix", expect_result[0])
3536
self.assertEqual("test.py", expect_result[1])
37+
38+
def test_simplify_path(self):
39+
self.assertEqual("/", simplify_path_v1("/../"))
40+
self.assertEqual("/home/foo", simplify_path_v1("/home//foo/"))
41+
self.assertEqual("/", simplify_path_v2("/../"))
42+
self.assertEqual("/home/foo", simplify_path_v2("/home//foo/"))

0 commit comments

Comments
 (0)