aboutsummaryrefslogtreecommitdiff
path: root/remove_adjacent_equal_char.py
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /remove_adjacent_equal_char.py
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'remove_adjacent_equal_char.py')
-rw-r--r--remove_adjacent_equal_char.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/remove_adjacent_equal_char.py b/remove_adjacent_equal_char.py
new file mode 100644
index 0000000..20a6b31
--- /dev/null
+++ b/remove_adjacent_equal_char.py
@@ -0,0 +1,29 @@
+# 2957. Remove Adjacent Almost-Equal Characters
+
+"""
+you are given a 0 indexed string 'word'. in one operation you can pick any
+index 'i' of 'word' and change 'word[i]' to any lowercase english letter.
+return the minimum number of operations needed to remove all adjacent almost
+equal characters from 'word'. two characters 'a' and 'b' are almost equal if
+'a == b' or 'a' and 'b' are adjacent in the alphabet
+"""
+
+
+class Solution(object):
+ def removeAlmostEqualCharacters(self, word):
+ """
+ :type word: str
+ :rtype: int
+ """
+ i, k = 1, 0
+ while i + k < len(word):
+ k += abs(ord(word[i + k]) - ord(word[i + k - 1])) < 2
+ i += 1
+ return k
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.removeAlmostEqualCharacters("aaaaa"))
+ print(obj.removeAlmostEqualCharacters("abddez"))
+ print(obj.removeAlmostEqualCharacters("zyxyxyz"))