Leetcode Problem 2645. Minimum Additions to Make Valid String

2645. Minimum Additions to Make Valid String

Leetcode Solutions

Iterative Pattern Matching

  1. Initialize a counter ans to 0 and an index pointer i to 0.
  2. While i is less than the length of the string minus 1: a. Get the current character c and the next character cnex. b. If c is 'a': i. If cnex is 'a', increment ans by 2. ii. Else if cnex is 'b' and the character after cnex is 'c', increment i by 2. iii. Else, increment ans by 1 and i by 1. c. If c is 'b': i. If cnex is 'c', increment ans by 1 and i by 1. ii. Else, increment ans by 2. d. If c is 'c', increment ans by 2. e. Increment i by 1.
  3. If i is less than the length of the string, increment ans by 2.
  4. Return ans.
UML Thumbnail

Pattern Matching with State Machine

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...