|
| 1 | +/* |
| 2 | +STRING KA KHEL |
| 3 | +Send Feedback |
| 4 | +Ninja started a gaming shop and for that shop, he is planning to make a new game ‘String Ka Khel’. In that game user is provided with ‘N’ number of strings and he has to find out the maximum length of strings he can form by joining these ‘N’ strings. For joining two strings there is a condition that two strings will only join if the last character of the first string is same as the last character of the second string. If the user will find out the correct answer he will be given Coding Ninja goodies by the Ninja. |
| 5 | +So your task is to find the maximum length of string which can be formed by joining strings according to the given condition. If no such combination exists return ‘0’. Strings only contain the letter ‘B’ and ‘R’. |
| 6 | +Example: |
| 7 | +The string is ‘RR’, ‘RB’ so we can combine ‘RR’ and ‘RB’ as the last character of ‘RR’ i.e ‘R’ matches with the first character of ‘RB’. But we cant combine ‘RB’ and ‘RR’ as the last character of ‘RB’ i.e ‘B’ doesn't matches with the first character of ‘RR’ i.e ‘R’ so our answer is '4'. |
| 8 | +Input Format: |
| 9 | +The first line of input contains an integer ‘T’ denoting the number of test cases. |
| 10 | +
|
| 11 | +The first line of each test case contains an integer ‘N’ denoting the number of strings. |
| 12 | +
|
| 13 | +The second line of each test case contains ‘N’ space-separated strings. |
| 14 | +Output Format: |
| 15 | +For each test case, print a single line containing a single integer denoting the maximum length of string which can be formed. In case no two strings can add simply print ‘0’. |
| 16 | +
|
| 17 | +The output of each test case will be printed in a separate line. |
| 18 | +Note: |
| 19 | +You do not need to print anything. It has already been taken care of. Just implement the given function. |
| 20 | +Constraints: |
| 21 | +1 <= T <= 100 |
| 22 | +2 <= N <= 1000 |
| 23 | +1 <= | ST | <= 1000 |
| 24 | +
|
| 25 | +Where ‘T’ represents the number of test cases and ‘N’ represents the total number of strings and '|ST|' represents the length of each of the ‘N’ strings. |
| 26 | +
|
| 27 | +Time Limit: 1 second |
| 28 | +Sample Input 1: |
| 29 | +2 |
| 30 | +3 |
| 31 | +RBR BBR RRR |
| 32 | +2 |
| 33 | +RRR BBB |
| 34 | +Sample Output 1: |
| 35 | +9 |
| 36 | +0 |
| 37 | +Explanation of Sample Input 1: |
| 38 | +Test Case 1: |
| 39 | +
|
| 40 | +So according to this test case ‘3’ strings are given ‘RBR’, ‘BBR’, ‘RRR’ so possible combinations are: |
| 41 | +
|
| 42 | +‘RBR’ + ‘RRR’ length is 6. |
| 43 | +‘RRR’ + ‘RBR’ length is 6. |
| 44 | +‘BBR’ + ‘RBR’ length is 6. |
| 45 | +‘BBR’ + ‘RRR’ length is 6. |
| 46 | +‘BBR’ + ‘RBR’ + ‘RRR’ length is 9. |
| 47 | +‘BBR’ + ‘RRR’ + ‘RBR’ length is 9. |
| 48 | +So we can choose between any two strings which have a length of ‘9’, hence ‘9’ is the maximum length possible. |
| 49 | +
|
| 50 | +Test Case 2: |
| 51 | +
|
| 52 | +There are two possible ways : |
| 53 | +‘RRR’, ‘BBB’ can't be possible because the last char is first(‘R’) != first char is second(‘B’). |
| 54 | +'BBB’, ‘RRR’ can't be possible because the last char of the first string (‘B’) != first char of the second string(‘R’). |
| 55 | +Sample Input 2 : |
| 56 | +2 |
| 57 | +3 |
| 58 | +BBR BBR BRB |
| 59 | +2 |
| 60 | +B B |
| 61 | +Sample Output 2 : |
| 62 | +6 |
| 63 | +2 |
| 64 | +*/ |
| 65 | + |
| 66 | + |
| 67 | +int stringKhel(vector<string>& arr, int n) |
| 68 | +{ |
| 69 | + int bb = 0, rb = 0, rr = 0, br = 0; |
| 70 | + int len = arr[0].size(); |
| 71 | + for (int i = 0; i < n; i++) |
| 72 | + { |
| 73 | + char first = arr[i][0]; |
| 74 | + char last = arr[i][len - 1]; |
| 75 | + |
| 76 | + if (first == 'R' && last == 'B') |
| 77 | + { |
| 78 | + rb++; |
| 79 | + } |
| 80 | + |
| 81 | + if (first == 'B' && last == 'R') |
| 82 | + { |
| 83 | + br++; |
| 84 | + } |
| 85 | + |
| 86 | + if (first == 'R' && last == 'R') |
| 87 | + { |
| 88 | + rr++; |
| 89 | + } |
| 90 | + |
| 91 | + if (first == 'B' && last == 'B') |
| 92 | + { |
| 93 | + bb++; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + int ans; |
| 98 | + if (br == 0 && rb == 0) |
| 99 | + { |
| 100 | + ans = max(bb, rr); |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + if (br == rb) |
| 105 | + { |
| 106 | + ans = rr + bb + 2 * min(br, rb); |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + ans = rr + bb + 2 * min(br, rb) + 1; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (ans == 1) |
| 115 | + { |
| 116 | + return 0; |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + return (ans * len); |
| 121 | + } |
| 122 | +} |
0 commit comments