FastPrepAlternating Signed Digit Sum

Alternating Signed Digit Sum

ZipRecruiter logoZipRecruiter● EasyNEW GRADOA
Learn

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) → int

Examples

Example 1

digits = "521"return = 4

The result is 5 - 2 + 1 = 4.

Example 2

digits = "12345"return = 3

The result is 1 - 2 + 3 - 4 + 5 = 3.

Constraints

  • 1 <= digits.length <= 100000
  • digits contains only decimal digits.

More ZipRecruiter problems

See ZipRecruiter hiring insights
public int alternatingDigitSum(String digits) {
    // Write your code here.
}
digits"521"
expected4
Checking account…