Python Array DSA Solutions Guide
Python Array DSA Solutions Guide
A string is identified as a palindrome if it reads the same backward as forward. This can be checked by comparing the string to its reverse using slicing (s[::-1]), resulting in a time complexity of O(n) and space complexity of O(n) when not done in-place since creating the reversed string requires extra space. Alternatively, comparing the string directly character by character from both ends to the middle allows O(n) time complexity and O(1) space complexity .
The first non-repeating character in a string can be identified by leveraging a frequency count of each character using a dictionary or a Counter from the collections module in Python. After populating the frequency data structure, a subsequent linear scan of the string can identify the first character with a frequency of one. This approach has a time complexity of O(n), where the cost of building the frequency map and the subsequent scan are both O(n), and space complexity of O(n) to store character counts .
To check if two strings are rotations of each other, concatenate the first string with itself and verify if the second string is a substring of this concatenated result. This method utilizes O(n) time complexity because checking if one string is a substring of another takes linear time in the combined length, and O(n) space is used to accommodate the concatenated string .
Kadane’s Algorithm is used to find the maximum sum of a contiguous subarray in an array of integers. It operates by iterating through the array, maintaining a current subarray sum, and updating the maximum sum found so far. Specifically, for each element at index i, the algorithm considers whether to add the element to the current subarray sum or start a new subarray starting at i, thus achieving O(n) time complexity and O(1) space complexity. This approach efficiently handles negative sums by resetting when a larger new subarray sum is detected .
Rotating an array by k positions involves rearranging the elements such that the elements shift to the right by k positions. This can be achieved by slicing: arr[-k:] + arr[:-k], which has time complexity O(n) and space complexity O(n). An in-place solution can reverse the whole array, reverse the first k elements, and finally reverse the rest, achieving O(1) space complexity while maintaining O(n) time complexity .
The longest common prefix of multiple strings can be determined using a vertical scanning approach, in which the prefix is iteratively constructed by comparing characters at each position across all strings. Starting with the first string as a reference, the algorithm checks each character position against others until a mismatch is found, thus building the prefix. This algorithm has a time complexity of O(n * m), where n is the minimum string length and m is the number of strings, since each character is checked once .
To find the second largest element in an array, a common approach is to remove duplicates, sort the array, and select the second last element, resulting in O(n log n) time complexity. This can be optimized to O(n) by traversing the array twice: first to find the largest element and second to find the largest element less than the maximum. This eliminates the need for sorting and uses only a constant amount of extra space .
In a sorted array, duplicates can be removed by traversing the array once and keeping a pointer for the position of the last unique element, achieving both O(n) time complexity and O(1) space complexity. For an unsorted array, duplicates can be removed by using a set to store seen elements, which requires O(n) time and O(n) space due to the necessity of using additional data structures to check for uniqueness throughout the traversal .
Reversing an array can be efficiently implemented in Python using slicing with the syntax arr[::-1]. This approach creates a reversed copy of the array, achieving a time complexity of O(n) and a space complexity of O(n). However, if the task is to reverse the array in-place using swapping, the space complexity reduces to O(1).
The intersection of two arrays can be computed using set operations in Python. By converting both arrays to sets, their intersection can be found using the '&' operator, resulting in a time complexity of O(n + m) and a space complexity of O(min(n, m)), where n and m are the lengths of the two arrays .