Posts

Showing posts with the label Two pointers

1. Valid palindrome

Image
Algorithm: Initialize two pointers, left at the start and right at the end of the string. Iterate until the left pointer is before the right pointer, skipping non-alphanumeric characters for both pointers. Compare characters positioned at the left and right pointers in lowercase; if they differ, return FALSE. Move left forward and right backward to check the next pair of characters. If the traversal completes without mismatches, return TRUE indicating the string is a palindrome. valid palindrome

Two Pointers

Image
  The two pointers pattern is a versatile technique used in problem-solving to efficiently traverse or manipulate sequential data structures, such as arrays or linked lists.  As the name suggests, it involves maintaining two pointers that traverse the data structure in a coordinated manner, typically starting from diffrent positions or moving in opossite directions.  These pointers dynamically adjust based on specific conditions or criteria, allowing for the efficient exploration of the data and enabling solutions with optimal time and space complexity.  Whenever there’s a requirement to find two data elements in an array that satisfy a certain condition, the two pointers pattern should be the first strategy to come to mind. The pointers can be used to iterate through the data structure in one or both directions, depending on the problem statement.  Valid Palindrome For example, to identify whether a string is a palindrome, we can use one pointer to i...