Minimum Length of String After Deleting Similar Ends

Minimum Length of String After Deleting Similar Ends

Please refer to LeetCode for the question details:

https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/description/

Intuition

We will use two pointers to solve this problem. Two pointer pattern is a handy tool and can help us in solving many simple-complex problems

Approach

  1. Create a left pointer and initialize with 0
  2. Create a right pointer and initialize it with array.length -1
  3. Start a while loop which will be valid until left < right
  4. If the characters at left and right are the same, continue the loop, else break.
  5. Until the left element is equal to its next elment, increment left by one.
  6. Until the right element is equal to its previous elment, decrement right by one.
  7. If left is euqal to right at this point, it means that we traversed the whole string according to the conditions in the questions and the lenght of the final string is zero. So, return zero at this point.
  8. If point 7 is not executed, increment left by one and decrement right by one.
  9. If the while loop ended, return right - left + 1

Complexity

  • Time complexity: O(n)
  • Space complexity: O(1)

Code

Here are the performance metrics:

Please consider upvoting my solution if you found it helpful!x

https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/solutions/4826022/beats-100-00-of-users-with-typescript-interview-solution

要查看或添加评论,请登录

Sajid Khan的更多文章

  • 141. Linked List Cycle

    141. Linked List Cycle

    Please refer to LeetCode for the question details: https://leetcode.com/problems/linked-list-cycle/description/ We are…

  • Bag of Tokens

    Bag of Tokens

    Please refer to LeetCode for the question descriptions. https://leetcode.

  • Maximum Odd Binary Number

    Maximum Odd Binary Number

    Of course, the month begins with a simple question, but it's important to attempt every challenge. In this question, we…

  • Even Odd Tree

    Even Odd Tree

    https://leetcode.com/problems/even-odd-tree/description/?envType=daily-question&envId=2024-02-29 If you look into the…

    1 条评论

社区洞察

其他会员也浏览了