Using javascript
const examples = [ "~O~O~O~O P", "P O~ O~ ~O O~", "~O~O~O~OP~O~OO~", "O~ O~~OPO~~O O~" ]; const putSpaces = item => item.replace(/(.{2})/g, "$1 "); const getDeafRats = example => { const [leftPad, rightPad] = example.replace(/\s+/g, "").split("P"); return ( (putSpaces(leftPad).match(/O~/g) || []).length + (putSpaces(rightPad).match(/~O/g) || []).length ); }; const totalDeafRats = examples.reduce( (total, result) => total + getDeafRats(result), 0 ); console.log("Total", totalDeafRats); console.log("For each example", examples.map(getDeafRats));
CodeSandbox example
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Using javascript
CodeSandbox example