面试题 17.05
Given an array filled with letters and numbers, find the longest subarray with an equal number of letters and numbers.
Return the subarray. If there are more than one answer, return the one which has the smallest index of its left endpoint. If there is no answer, return an empty arrary.
Example 1:
Input: ["A","1","B","C","D","2","3","4","E","5","F","G","6","7","H","I","J","K","L","M"]
Output: ["A","1","B","C","D","2","3","4","E","5","F","G","6","7"] Example 2:
Input: ["A","A"]
Output: [] Note:
array.length <= 100000
Solutions
prefixsum and hashmap O(n)
The number of characters or numbers in
array(i:j]
can be calculated inO(1)
time based on theprefix countsum
ofarray[:i]
andarray[:j]
.
Last updated
Was this helpful?