Given an array nums and a value target, find all unique triplets (a, b, c) drawn from three distinct indices i < j < k such that nums[i] + nums[j] + nums[k] equals target. Return the list of triplets, with no duplicate triplet appearing more than once in the output.
Two triplets are considered duplicates if they consist of the same three values in the same sorted order. To enforce uniqueness, sort the array first, then iterate the first element while skipping repeats, and use two pointers from opposite ends to find the remaining pair, also skipping over equal neighbors.
If target is not a number that allows any valid sum (for example, a non-numeric value), the result should be an empty list.
Input
nums: an array of values (typically integers)
target: the desired sum
Output
An array of triplets [a, b, c] where a + b + c == target. Each unique triplet appears at most once.
Examples
Example 1
Inputnums = [1, 2, 3, 4, 5], target = "hello"
Output[]
Explanation: No triplet of integers can sum to a non-numeric value, so the result is empty.
Example 2
Inputnums = [1, 1, 3, 4, 5, 9], target = "abcde"
Output[]
Example 3
Inputnums = [10, 20], target = "a"
Output[]
Explanation: Fewer than three elements means no triplet can be formed.
Constraints
- 0 <= nums.length
- The output must contain no duplicate triplets.