FastPrepMinimum Digit Changes Between String Halves

Minimum Digit Changes Between String Halves

JP Morgan Chase logoJP Morgan Chase● EasyINTERNNEW GRADOA
Learn

Problem statement

You are given an even-length string s that contains digits only. Split it into two equal halves.

In one operation, choose any position in the first half and replace its digit with any other digit from 0 through 9.

Return the minimum number of operations needed to make the first half an anagram of the second half.

Function

getAnagram(s: String) → int

Examples

Example 1

s = "123212"return = 1

The halves are 123 and 212. Replace digit 3 in the first half with 2, producing 122, which is an anagram of 212.

Example 2

s = "123456"return = 3

The halves 123 and 456 share no digit. Every position in the first half must be replaced, so the minimum is 3.

Constraints

  • 1 <= s.length <= 10^5
  • s.length is a multiple of 2.
  • s contains digits only.

More JP Morgan Chase problems

See JP Morgan Chase hiring insights
public int getAnagram(String s) {
  // write your code here
}
s"123212"
expected1
Checking account…