From f4d93445bc73167cd292cfcfd97728a5380e8cb7 Mon Sep 17 00:00:00 2001 From: Jimit Dholakia <25223469+jimit105@users.noreply.github.com> Date: Tue, 5 Mar 2024 05:34:51 +0000 Subject: [PATCH] Sync LeetCode submission Runtime - 60 ms (89.35%), Memory - 17.2 MB (84.27%) --- .../README.md | 49 +++++++++++++++++++ .../solution.py | 17 +++++++ 2 files changed, 66 insertions(+) create mode 100644 1850-minimum-length-of-string-after-deleting-similar-ends/README.md create mode 100644 1850-minimum-length-of-string-after-deleting-similar-ends/solution.py diff --git a/1850-minimum-length-of-string-after-deleting-similar-ends/README.md b/1850-minimum-length-of-string-after-deleting-similar-ends/README.md new file mode 100644 index 0000000..b757195 --- /dev/null +++ b/1850-minimum-length-of-string-after-deleting-similar-ends/README.md @@ -0,0 +1,49 @@ +

Given a string s consisting only of characters 'a', 'b', and 'c'. You are asked to apply the following algorithm on the string any number of times:

+ +
    +
  1. Pick a non-empty prefix from the string s where all the characters in the prefix are equal.
  2. +
  3. Pick a non-empty suffix from the string s where all the characters in this suffix are equal.
  4. +
  5. The prefix and the suffix should not intersect at any index.
  6. +
  7. The characters from the prefix and suffix must be the same.
  8. +
  9. Delete both the prefix and the suffix.
  10. +
+ +

Return the minimum length of s after performing the above operation any number of times (possibly zero times).

+ +

 

+

Example 1:

+ +
+Input: s = "ca"
+Output: 2
+Explanation: You can't remove any characters, so the string stays as is.
+
+ +

Example 2:

+ +
+Input: s = "cabaabac"
+Output: 0
+Explanation: An optimal sequence of operations is:
+- Take prefix = "c" and suffix = "c" and remove them, s = "abaaba".
+- Take prefix = "a" and suffix = "a" and remove them, s = "baab".
+- Take prefix = "b" and suffix = "b" and remove them, s = "aa".
+- Take prefix = "a" and suffix = "a" and remove them, s = "".
+ +

Example 3:

+ +
+Input: s = "aabccabba"
+Output: 3
+Explanation: An optimal sequence of operations is:
+- Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb".
+- Take prefix = "b" and suffix = "bb" and remove them, s = "cca".
+
+ +

 

+

Constraints:

+ + diff --git a/1850-minimum-length-of-string-after-deleting-similar-ends/solution.py b/1850-minimum-length-of-string-after-deleting-similar-ends/solution.py new file mode 100644 index 0000000..72c9697 --- /dev/null +++ b/1850-minimum-length-of-string-after-deleting-similar-ends/solution.py @@ -0,0 +1,17 @@ +# Approach 1: Two Pointers + +class Solution: + def minimumLength(self, s: str) -> int: + begin, end = 0, len(s) - 1 + + while begin < end and s[begin] == s[end]: + c = s[begin] + + while begin <= end and s[begin] == c: + begin += 1 + + while end > begin and s[end] == c: + end -= 1 + + return end - begin + 1 +