5 Best Ways to Count Number of Islands in a Given Matrix in Python

Counting Number of Islands in a Matrix Using Python πŸ’‘ Problem Formulation: The challenge involves writing a program to count the number of ‘islands’ in a 2D matrix, where an island is formed by adjacent (‘up’, ‘down’, ‘left’, ‘right’) 1s and surrounded by 0s. For example, the input matrix may look like [[1,1,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]], where this … Read more

5 Best Ways to Count Number of Palindromic Substrings in Python

πŸ’‘ Problem Formulation: Given a string, the task is to count all the palindromic substrings within it. A palindromic string is one that reads the same backward as forward, like “radar” or “level”. For example, given the input “abbaeae”, the desired output would be 6, as there are six substrings (“abb”, “bb”, “abba”, “aea”, “eae”, … Read more

5 Best Ways to Check All Palindromic Substrings for Odd Lengths in Python

πŸ’‘ Problem Formulation: This article explores different methods to determine if all palindromic substrings within a given string have odd lengths. The problem requires us to write a function that takes a string as input and returns a boolean value: True if every palindromic substring is of odd length, and False otherwise. For instance, given … Read more

5 Best Ways to Check if Two Strings Are at Most One Edit Distance Apart in Python

πŸ’‘ Problem Formulation: In the context of string processing, the “edit distance” between two strings refers to the minimum number of operations needed to transform one string into another. Operations can include insertions, deletions, or substitutions of characters. This article explores how to determine if two strings are zero or one edit away from being … Read more