String Without 3 Identical Consecutive Letters

Given a string S having lowercase English letters, returns a string with no instances of three identical consecutive letters, obtained from S by deleting the minimum possible number of letters.

Example 1:

Input: eedaaad

Output: eedaad

Explanation:

One occurrence of letter a is deleted.

Example 2:

Input: xxxtxxx

Output: xxtxx

Explanation:

Note that letter x can occur more than three times in the returned string if the occurrences are not consecutive.

Example 3:

Input: uuuuxaaaaxum

Output: uuxaaxum

Try it yourself

Implementation

1
1
def filterString(s: str) -> str:
2
-
      # WRITE YOUR BRILLIANT CODE HERE
2
+
      news = s[0:2]
3
+
      for i in range(2, len(s)):
4
+
          if s[i] == s[i-1] and s[i] == s[i-2]:
5
+
              # Do not append if the previous chars are the same
6
+
              continue
7
+
          else:
8
+
              news += s[i]
9
+
      return news
3
10
if __name__ == '__main__':
4
11
    print(filterString(input()))