Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please solve in typescript. Thank you. There is a road consisting of N segments, numbered from 0 to N - 1 , represented by a

Please solve in typescript. Thank you.
There is a road consisting of N segments, numbered from 0 to N-1, represented by a string S . Segment S[K] of the road may contain a pothole, denoted by a single uppercase " x " character, or may be a good segment without
any potholes, denoted by a single dot, ".2.
For example, string ". x .." means that there are two potholes in total in the road: one is located in segment S[1] and one in segment S[4]. All other segments are good.
The road fixing machine can patch over three consecutive segments at once with asphalt and repair all the potholes located within each of these segments. Good or already repaired segments remain good after patching them.
Your task is to compute the minimum number of patches required to repair all the potholes in the road.
Write a function:
function solution(S);
that, given a string S of length N , returns the minimum number of patches required to repair all the potholes.
Examples:
Given S=".x.. x", your function should return 2. The road fixing machine could patch, for example, segments 0-2 and 2-4.
Given S="x.xxxxx. x.", your function should return 3. The road fixing machine could patch, for example, segments 0-2,3-5 and 6-8.
Given S="
xx.xxx..", your function should return 2. The road fixing machine could patch, for example, segments 0-2 and 3-5.
Given S="xxxx", your function should return 2. The road fixing machine could patch, for example, segments 0-2 and 1-3.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [3..100,000];
string S is made only of the characters '.' and/or 'x'.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

3rd Edition

978-1119907466

More Books

Students also viewed these Databases questions

Question

Understand the purpose and methods of cross-cultural training

Answered: 1 week ago