Alternating Signed Digit Sum
Problem statement
You are given a nonempty decimal string digits. Read it from left to right using zero-based positions.
Add digits at even positions and subtract digits at odd positions. Return the resulting signed sum.
Function
alternatingDigitSum(digits: String) → intExamples
Example 1
digits = "521"return = 4The result is 5 - 2 + 1 = 4.
Example 2
digits = "12345"return = 3The result is 1 - 2 + 3 - 4 + 5 = 3.
Constraints
1 <= digits.length <= 100000digitscontains only decimal digits.