Mars Exploration

Mars Exploration

Sami’s spaceship crashed on Mars! She sends a series of SOS messages to Earth for help.

Letters in some of the SOS messages are altered by cosmic radiation during transmission. Given the signal received by Earth as a string, s, determine how many letters of Sami’s SOS have been changed by radiation.

For example, Earth receives SOSTOT. Sami’s original message was SOSSOS. Two of the message characters were changed in transit.

Function Description

Complete the marsExploration function in the editor below. It should return an integer representing the number of letters changed during transmission.

marsExploration has the following parameter(s):

  • s: the string as received on Earth

Input Format

There is one line of input: a single string, s.

Note: As the original message is just SOS repeated n times, s‘s length will be a multiple of 3.

Constraints

  • 1 <= |s| <= 99
  • |s| % 3 = 0
  • s will contain only uppercase English letters, ascii[A-Z].

Output Format

Print the number of letters in Sami’s message that were altered by cosmic radiation.

Sample Input 0

1
SOSSPSSQSSOR

Sample Output 0

1
3

Explanation 0

s = SOSSPSSQSSOR, and signal length |s| = 12. Sami sent 4 SOS messages (i.e.: 12/3 = 4).

1
2
3
Expected signal: SOSSOSSOSSOS
Recieved signal: SOSSPSSQSSOR
Difference: X X X

We print the number of changed letters.

Sample Input 1

1
SOSSOT

Sample Output 1

1
1

Explanation 1

s = SOSSOT, and signal length |s| = 6. Sami sent 2 SOS messages (i.e.: 6/3 = 2).

1
2
3
Expected Signal: SOSSOS     
Received Signal: SOSSOT
Difference: X

We print the number of changed letters, which is 1.

Sample Input 2

1
SOSSOSSOS

Sample Output 2

1
0

Explanation 2

Since no character is altered, we print 0.


Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Complete the marsExploration function below.
function marsExploration(s) {
const signal = 'SOS';
let count = 0;

for (let i = 0, { length } = s; i < length; i += 3) {
const focus = s.slice(i, i + 3);

(focus[0] !== signal[0]) && (count += 1);
(focus[1] !== signal[1]) && (count += 1);
(focus[2] !== signal[2]) && (count += 1);
}

return count;
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×