From 600eddb79137084dbc6a50464b628a96d5932085 Mon Sep 17 00:00:00 2001 From: Spas Z Date: Sun, 24 Oct 2021 15:37:53 +0100 Subject: [PATCH 1/7] adding problem solution --- 0394/Readme.md | 29 +++++++++++++++++++++++++++++ 0394/decode-string.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 0394/Readme.md create mode 100644 0394/decode-string.py diff --git a/0394/Readme.md b/0394/Readme.md new file mode 100644 index 0000000..775064d --- /dev/null +++ b/0394/Readme.md @@ -0,0 +1,29 @@ +# 204. Count Primes + +Given an integer n, return the number of prime numbers that are strictly less than n. + +### Examples: + +Example 1: + + Input: n = 10 + Output: 4 + Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. + + +Example 2: + + Input: n = 0 + Output: 0 + +Example 3: + + Input: n = 1 + Output: 0 + + +## Constraints: + + 0 <= n <= 5 * 106 + +#### Question link : [204_count_primes](https://leetcode.com/problems/count-primes/) \ No newline at end of file diff --git a/0394/decode-string.py b/0394/decode-string.py new file mode 100644 index 0000000..5c688c5 --- /dev/null +++ b/0394/decode-string.py @@ -0,0 +1,31 @@ +class Solution: + + def extract_digit(self, i: int, s: str): + digit = "" + + while (s[i] is not "["): + digit += s[i] + i +=1 + + # i + 1 to skip the [ + return i + 1, digit + + def decode_word(self, i: int, s: str): + + concat = "" + while(i < len(s) and s[i] is not "]"): + if (s[i].isdigit()): + i, digit = self.extract_digit(i, s) + + i, word = self.decode_word(i, s) + concat += int(digit) * word + else: + concat += s[i] + i += 1 + return i + 1, concat + + def decodeString(self, s: str) -> str: + + i, word = self.decode_word(0, s) + + return word \ No newline at end of file From b0cdf33095f471e3f03853f1016b004a351659b6 Mon Sep 17 00:00:00 2001 From: Spas Z Date: Sun, 24 Oct 2021 15:41:29 +0100 Subject: [PATCH 2/7] updated readme --- 0394/Readme.md | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/0394/Readme.md b/0394/Readme.md index 775064d..9497dc4 100644 --- a/0394/Readme.md +++ b/0394/Readme.md @@ -1,29 +1,38 @@ -# 204. Count Primes +# 394. Decode String -Given an integer n, return the number of prime numbers that are strictly less than n. +Given an encoded string, return its decoded string. -### Examples: +The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. -Example 1: +You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc. - Input: n = 10 - Output: 4 - Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. +Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4]. +### Examples: +Example 1: + + Input: s = "3[a]2[bc]" + Output: "aaabcbc" Example 2: - Input: n = 0 - Output: 0 + Input: s = "3[a2[c]]" + Output: "accaccacc" Example 3: - Input: n = 1 - Output: 0 - + Input: s = "2[abc]3[cd]ef" + Output: "abcabccdcdcdef" + +Example 4: + Input: s = "abc3[cd]xyz" + Output: "abccdcdcdxyz" ## Constraints: - 0 <= n <= 5 * 106 + 1 <= s.length <= 30 + s consists of lowercase English letters, digits, and square brackets '[]'. + s is guaranteed to be a valid input. + All the integers in s are in the range [1, 300]. -#### Question link : [204_count_primes](https://leetcode.com/problems/count-primes/) \ No newline at end of file +#### Question link : [394_decode_string](https://leetcode.com/problems/decode-string/) \ No newline at end of file From 508eb12555a12955cf69b3e32712ccde7b7eddb6 Mon Sep 17 00:00:00 2001 From: Spas_Z Date: Wed, 27 Oct 2021 19:28:10 +0100 Subject: [PATCH 3/7] fixing linting issues --- 0394/decode-string.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/0394/decode-string.py b/0394/decode-string.py index 5c688c5..52b1d0e 100644 --- a/0394/decode-string.py +++ b/0394/decode-string.py @@ -3,17 +3,17 @@ class Solution: def extract_digit(self, i: int, s: str): digit = "" - while (s[i] is not "["): + while (s[i] != "["): digit += s[i] - i +=1 - + i += 1 + # i + 1 to skip the [ return i + 1, digit def decode_word(self, i: int, s: str): concat = "" - while(i < len(s) and s[i] is not "]"): + while(i < len(s) and s[i] != "]"): if (s[i].isdigit()): i, digit = self.extract_digit(i, s) @@ -23,9 +23,10 @@ def decode_word(self, i: int, s: str): concat += s[i] i += 1 return i + 1, concat - + def decodeString(self, s: str) -> str: i, word = self.decode_word(0, s) - return word \ No newline at end of file + return word + From ece43bfbf2d938f7dd92881a11931c147ca03f38 Mon Sep 17 00:00:00 2001 From: Spas_Z Date: Wed, 27 Oct 2021 19:31:02 +0100 Subject: [PATCH 4/7] Removing all blank spaces for linting --- 0394/decode-string.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/0394/decode-string.py b/0394/decode-string.py index 52b1d0e..de0ee5f 100644 --- a/0394/decode-string.py +++ b/0394/decode-string.py @@ -1,32 +1,23 @@ class Solution: - def extract_digit(self, i: int, s: str): digit = "" - while (s[i] != "["): digit += s[i] i += 1 - # i + 1 to skip the [ return i + 1, digit - def decode_word(self, i: int, s: str): - concat = "" while(i < len(s) and s[i] != "]"): if (s[i].isdigit()): i, digit = self.extract_digit(i, s) - i, word = self.decode_word(i, s) concat += int(digit) * word else: concat += s[i] i += 1 return i + 1, concat - def decodeString(self, s: str) -> str: - i, word = self.decode_word(0, s) - return word From ea4273cae26d28dfda7715164d47cad165f9b86c Mon Sep 17 00:00:00 2001 From: Spas_Z Date: Wed, 27 Oct 2021 19:33:04 +0100 Subject: [PATCH 5/7] Adding blank lines between methods --- 0394/decode-string.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/0394/decode-string.py b/0394/decode-string.py index de0ee5f..37077b4 100644 --- a/0394/decode-string.py +++ b/0394/decode-string.py @@ -6,6 +6,7 @@ def extract_digit(self, i: int, s: str): i += 1 # i + 1 to skip the [ return i + 1, digit + def decode_word(self, i: int, s: str): concat = "" while(i < len(s) and s[i] != "]"): @@ -17,7 +18,7 @@ def decode_word(self, i: int, s: str): concat += s[i] i += 1 return i + 1, concat + def decodeString(self, s: str) -> str: i, word = self.decode_word(0, s) return word - From 37ee9932f9ec67b3971ecfdc135ebd61960e8641 Mon Sep 17 00:00:00 2001 From: Spas_Z Date: Wed, 27 Oct 2021 19:37:02 +0100 Subject: [PATCH 6/7] removing tabs for linting --- 0394/decode-string.py | 45 +++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/0394/decode-string.py b/0394/decode-string.py index 37077b4..5f63343 100644 --- a/0394/decode-string.py +++ b/0394/decode-string.py @@ -1,24 +1,23 @@ -class Solution: - def extract_digit(self, i: int, s: str): - digit = "" - while (s[i] != "["): - digit += s[i] +def extract_digit(self, i: int, s: str): + digit = "" + while (s[i] != "["): + digit += s[i] + i += 1 + # i + 1 to skip the [ + return i + 1, digit + +def decode_word(self, i: int, s: str): + concat = "" + while(i < len(s) and s[i] != "]"): + if (s[i].isdigit()): + i, digit = self.extract_digit(i, s) + i, word = self.decode_word(i, s) + concat += int(digit) * word + else: + concat += s[i] i += 1 - # i + 1 to skip the [ - return i + 1, digit - - def decode_word(self, i: int, s: str): - concat = "" - while(i < len(s) and s[i] != "]"): - if (s[i].isdigit()): - i, digit = self.extract_digit(i, s) - i, word = self.decode_word(i, s) - concat += int(digit) * word - else: - concat += s[i] - i += 1 - return i + 1, concat - - def decodeString(self, s: str) -> str: - i, word = self.decode_word(0, s) - return word + return i + 1, concat + +def decodeString(self, s: str) -> str: + i, word = self.decode_word(0, s) + return word From 20568436ef7ce9fff4ab3beabd898b04e1e48881 Mon Sep 17 00:00:00 2001 From: Spas_Z Date: Wed, 27 Oct 2021 19:38:51 +0100 Subject: [PATCH 7/7] adding second blank line for linting --- 0394/decode-string.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/0394/decode-string.py b/0394/decode-string.py index 5f63343..4291503 100644 --- a/0394/decode-string.py +++ b/0394/decode-string.py @@ -6,6 +6,7 @@ def extract_digit(self, i: int, s: str): # i + 1 to skip the [ return i + 1, digit + def decode_word(self, i: int, s: str): concat = "" while(i < len(s) and s[i] != "]"): @@ -18,6 +19,7 @@ def decode_word(self, i: int, s: str): i += 1 return i + 1, concat + def decodeString(self, s: str) -> str: i, word = self.decode_word(0, s) return word