Blake is a teacher organizing the Independence Day parade. There are N students in a…
- User
Blake is a teacher organizing the Independence Day parade. There are N students in a line, some dressed formally and others informally. Blake's goal is to make sure all the students are dressed formally. She selects the first student who is dressed informally and provide him with formal clothes.
But since she is in a hurry, she picks the entire continuous segment that covers all the informally dressed students and remove them from the parade in order to start the
parade quickly.
You are given a string where:
•F represents a student dressed formally,
• T represents a student dressed informally.
Your task is to find and return the minimum length of the segment Blake needs to remove to ensure all the students are dressed formally.
Note: There is at least one student in the line always.
Input Specification:
input1: An integer value N representing the length of the students lined up.
input2: A string value representing the dresses of the N students lined up.
Output Specification:
Return the minimum length of the segment Blake needs to remove to ensure all the students are dressed formally.import java.io.;
import java.util.;
ramy
// Read only region start
class UserMainCode
{
public int longestSegment(int input1, String input2){
8
// Read only region end
9
10
11
12
13
} complete the function in java
- Assistant
To solve this problem, we need to find the minimum continuous segment that covers all the students dressed informally (represented by 'T' in the string). This segment starts from the position of the first 'T' and ends at the position of the last 'T'.
Here's the approach:
-
Initialize two variables,
first_T_indexandlast_T_index, to keep track of the positions of the first and last 'T' in the string. -
Iterate through the string:
- When you find a 'T', check if
first_T_indexis-1(which means it's the first 'T' you're encountering). If it is, assign the current index tofirst_T_index. - Always update when you find a 'T' to ensure it ends up being the position of the last 'T' in the string.
- When you find a 'T', check if
-