Correct the mistakes of the character recognition software

Lee·2022년 8월 19일
0

Algorithm

목록 보기
75/92
post-thumbnail

❓ Return Negative

Q. Character recognition software is widely used to digitise printed texts. Thus the texts can be edited, searched and stored on a computer.

When documents (especially pretty old ones written with a typewriter), are digitised character recognition softwares often make mistakes.

Your task is correct the errors in the digitised text. You only have to handle the following mistakes:

S is misinterpreted as 5
O is misinterpreted as 0
I is misinterpreted as 1
The test cases contain numbers only by mistake.

✔ Solution

//#my solution
function correct(string) {
  const mistake = {5: 'S', 0: 'O', 1: 'I'}
  const replace = function(str) {
    return str in mistake ? mistake[str] : str;
  }
  return string.split('').map(replace).join('');
}

//#other solution
correct = s => s.replace(/0/g,'O').replace(/1/g,'I').replace(/5/g,'S')
profile
Lee

0개의 댓글