Given two strings S and T, where S represents the first lane in which vehicles move from left to right and T represents the second lane in which vehicles move from right to left. Vehicles can be either B (bike), C (car), or T (truck). The task is to find the probability of collision between two trucks.
Examples:
Input: S = “TCCBCTTB”, T = “BTCCBBTT”
Output: 0.194444
Explanation:
Total collision = 7
Total accident = 36
Therefore, the probability can be calculated by 7/36 = 0.19444.Input: S = “BTT”, T = “BTCBT”
Output: 0.25000
Illustration:
S = "TCCBCTTB", T = "BTCCBBTT" Possible cases | Accidents | Collision ----------------------------------------- TCCBCTTB | | BTCCBBTT | 8 | 1 | | TCCBCTTB | | BTCCBBTT | 7 | 3 | | TCCBCTTB | | BTCCBBTT | 6 | 1 | | TCCBCTTB | | BTCCBBTT | 5 | 0 | | TCCBCTTB | | BTCCBBTT | 4 | 0 | | TCCBCTTB | | BTCCBBTT | 3 | 0 | | TCCBCTTB | | BTCCBBTT | 2 | 1 | | TCCBCTTB | | BTCCBBTT | 1 | 1 Total number of accidents: 8+7+6+5+4+3+2+1=36 Total number of collision: 1+3+1+0+0+0+1+1=7 Probability: 7/36=0.19444
Approach: Follow the steps below to solve the problem:
- Find the total number of favorable outcomes as the total number of collisions(accidents between trucks) and the total number of possible outcomes(total number of collisions) as the total number of accidents.
- Initialize a variable answer equals to 0 to stores the count of collisions.
- Count the number of trucks in the string T and store it in a variable count.
- Iterate over the characters of the strings S and T simultaneously:
- If S[i] is equal to ‘T’, increment answer by count.
- If T[i] is equal to ‘T’, decrement the count by 1.
- Now, calculate the total number of possible outcomes (total number of accidents). It is the sum of all the length of overlapping if keep shifting string a towards right or string b towards left by one unit.
- Let the length of string be N and string b be M. Then, the total number of overlapping will be:
- If N > M then, it will be the sum of first M natural numbers i.e., M*(M + 1)/2.
- Otherwise, it will be N*(N + 1)/2 + (M – N)*N.
- Find the probability as the ratio of the count of the collisions and the count of the accidents.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate total // number of accidents double count_of_accident(string a, string b) { // String size int n = a.size(), m = b.size(); if (n > m) return (m * (m + 1)) / 2; else return (n * (n + 1)) / 2 + (m - n) * n; } // Function to calculate count // of all possible collision double count_of_collision(string a, string b) { int n = a.size(), m = b.size(); // Stores the count of collisions int answer = 0; // Total number of truck in lane b int count_of_truck_in_lane_b = 0; for ( int i = 0; i < m; i++) if (b[i] == 'T' ) count_of_truck_in_lane_b++; // Count total number of collisions // while traversing the string a for ( int i = 0; i < n && i < m; i++) { if (a[i] == 'T' ) answer += count_of_truck_in_lane_b; if (b[i] == 'T' ) count_of_truck_in_lane_b--; } return answer; } // Function to calculate the // probability of collisions double findProbability(string a, string b) { // Evaluate total outcome that is // all the possible accident double total_outcome = count_of_accident(a, b); // Evaluate favourable outcome i.e., // count of collision of trucks double favourable_outcome = count_of_collision(a, b); // Print desired probability cout << favourable_outcome / total_outcome; } // Driver Code int main() { string S = "TCCBCTTB" , T = "BTCCBBTT" ; // Function Call findProbability(S, T); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG { // Function to calculate total // number of accidents static int count_of_accident(String a, String b) { // String size int n = a.length(), m = b.length(); if (n > m) return (m * (m + 1 )) / 2 ; else return (n * (n + 1 )) / 2 + (m - n) * n; } // Function to calculate count // of all possible collision static double count_of_collision(String a, String b) { int n = a.length(), m = b.length(); // Stores the count of collisions double answer = 0 ; // Total number of truck in lane b int count_of_truck_in_lane_b = 0 ; for ( int i = 0 ; i < m; i++) if (b.charAt(i) == 'T' ) count_of_truck_in_lane_b++; // Count total number of collisions // while traversing the String a for ( int i = 0 ; i < n && i < m; i++) { if (a.charAt(i) == 'T' ) answer += count_of_truck_in_lane_b; if (b.charAt(i) == 'T' ) count_of_truck_in_lane_b--; } return answer; } // Function to calculate the // probability of collisions static void findProbability(String a, String b) { // Evaluate total outcome that is // all the possible accident int total_outcome = count_of_accident(a, b); // Evaluate favourable outcome i.e., // count of collision of trucks double favourable_outcome = count_of_collision(a, b); // Print desired probability System.out.printf( "%4f" ,favourable_outcome / total_outcome); } // Driver Code public static void main(String[] args) { String S = "TCCBCTTB" , T = "BTCCBBTT" ; // Function Call findProbability(S, T); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 program for the above approach # Function to calculate total # number of accidents def count_of_accident(a, b): n = len (a) m = len (b) if (n > m): return (m * (m + 1 )) / 2 else : return ((n * (n + 1 )) / 2 + (m - n) * n) # Function to calculate count # of all possible collision def count_of_collision(a, b): # Size of string n = len (a) m = len (b) # Stores the count of collisions answer = 0 # Total number of truck in lane b count_of_truck_in_lane_b = 0 for i in range ( 0 , m): if (b[i] = = 'T' ): count_of_truck_in_lane_b + = 1 # Count total number of collisions # while traversing the string a i = 0 while (i < m and i < n): if (a[i] = = 'T' ): answer + = count_of_truck_in_lane_b if (b[i] = = 'T' ): count_of_truck_in_lane_b - = 1 i + = 1 return answer # Function to calculate the # probability of collisions def findProbability(a, b): # Evaluate total outcome that is # all the possible accident total_outcome = count_of_accident(a, b); # Evaluate favourable outcome i.e., # count of collision of trucks favourable_outcome = count_of_collision(a, b); # Print desired probability print (favourable_outcome / total_outcome) # Driver Code if __name__ = = "__main__" : S = "TCCBCTTB" T = "BTCCBBTT" # Function Call findProbability(S, T) # This code is contributed by Virusbuddah_ |
C#
// C# program for the above approach using System; class GFG { // Function to calculate total // number of accidents static int count_of_accident(String a, String b) { // String size int n = a.Length, m = b.Length; if (n > m) return (m * (m + 1)) / 2; else return (n * (n + 1)) / 2 + (m - n) * n; } // Function to calculate count // of all possible collision static double count_of_collision(String a, String b) { int n = a.Length, m = b.Length; // Stores the count of collisions double answer = 0; // Total number of truck in lane b int count_of_truck_in_lane_b = 0; for ( int i = 0; i < m; i++) if (b[i] == 'T' ) count_of_truck_in_lane_b++; // Count total number of collisions // while traversing the String a for ( int i = 0; i < n && i < m; i++) { if (a[i] == 'T' ) answer += count_of_truck_in_lane_b; if (b[i] == 'T' ) count_of_truck_in_lane_b--; } return answer; } // Function to calculate the // probability of collisions static void findProbability(String a, String b) { // Evaluate total outcome that is // all the possible accident int total_outcome = count_of_accident(a, b); // Evaluate favourable outcome i.e., // count of collision of trucks double favourable_outcome = count_of_collision(a, b); // Print desired probability Console.Write( "{0:F4}" , favourable_outcome / total_outcome); } // Driver Code public static void Main(String[] args) { String S = "TCCBCTTB" , T = "BTCCBBTT" ; // Function Call findProbability(S, T); } } // This code is contributed by sapnasingh4991 |
Javascript
<script> // Javascript program to implement // the above approach // Function to calculate total // number of accidents function count_of_accident(a, b) { // String size let n = a.length, m = b.length; if (n > m) return (m * (m + 1)) / 2; else return (n * (n + 1)) / 2 + (m - n) * n; } // Function to calculate count // of all possible collision function count_of_collision(a, b) { let n = a.length, m = b.length; // Stores the count of collisions let answer = 0; // Total number of truck in lane b let count_of_truck_in_lane_b = 0; for (let i = 0; i < m; i++) if (b[i] == 'T' ) count_of_truck_in_lane_b++; // Count total number of collisions // while traversing the String a for (let i = 0; i < n && i < m; i++) { if (a[i] == 'T' ) answer += count_of_truck_in_lane_b; if (b[i] == 'T' ) count_of_truck_in_lane_b--; } return answer; } // Function to calculate the // probability of collisions function findProbability(a, b) { // Evaluate total outcome that is // all the possible accident let total_outcome = count_of_accident(a, b); // Evaluate favourable outcome i.e., // count of collision of trucks let favourable_outcome = count_of_collision(a, b); // Print desired probability document.write( favourable_outcome / total_outcome); } // Driver Code let S = "TCCBCTTB" , T = "BTCCBBTT" ; // Function Call findProbability(S, T); // This code is contributed by souravghosh0416. </script> |
0.194444
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!