5 Best Ways to Find the Length of the Longest Substring with Two Distinct Elements in Python

πŸ’‘ Problem Formulation: In this article, we tackle the challenge of identifying the longest substring within a given string that contains no more than two unique characters. This problem often appears in coding interviews and algorithmic challenges. For instance, given the input string “aabcbcbb”, the desired output would be 5, referring to the substring “bcbbb” … Read more

5 Best Ways to Find the Length of the Longest Increasing Subsequence in Python

πŸ’‘ Problem Formulation: Finding the longest increasing subsequence (LIS) is a common algorithmic challenge which involves identifying the longest subsequence of a given sequence of numbers, where the subsequence’s elements are in sorted order, ascending, and strictly increasing. For instance, given the array [10, 9, 2, 5, 3, 7, 101, 18], the length of the … Read more

5 Best Ways to Check Minimum Number of Characters Needed to Make String Palindrome in Python

πŸ’‘ Problem Formulation: How can we calculate the minimum number of characters required to be inserted into a string to transform it into a palindrome? For example, given the input string ‘abca’, the desired output would be 1, since inserting ‘b’ after ‘a’ (resulting in ‘abcba’) makes it a palindrome. Method 1: Dynamic Programming This … Read more

5 Best Ways to Traverse a Binary Tree Level-Wise in an Alternating Way in Python

πŸ’‘ Problem Formulation: Traversing a binary tree in a level-wise alternating fashion means visiting nodes level by level, starting from the root, but with the direction of traversal switching between levels. Assume we have an input binary tree, the desired output would be a list containing nodes’ values alternating between left-to-right and right-to-left order for … Read more