|
1 | | -/* |
2 | | -Problem Statement: Maximum weight transformation of a given string |
3 | | -Given a string consisting of only A’s and B’s. We can transform the given string to another string by toggling any character. Thus many transformations of the given string are possible. The task is to find Weight of the maximum weight transformation. |
4 | 1 |
|
5 | | -Weight of a sting is calculated using below formula. |
| 2 | +/** |
| 3 | + * Problem Statement: Maximum weight transformation of a given string |
| 4 | + * Given a string consisting of only A’s and B’s. We can transform the given string to another string by toggling any character. Thus many transformations of the given string are possible. The task is to find Weight of the maximum weight transformation. |
| 5 | + * <p> |
| 6 | + * Weight of a sting is calculated using below formula. |
| 7 | + * <p> |
| 8 | + * Weight of string = Weight of total pairs + |
| 9 | + * weight of single characters - |
| 10 | + * Total number of toggles. |
| 11 | + * <p> |
| 12 | + * Two consecutive characters are considered as pair only if they |
| 13 | + * are different. |
| 14 | + * Weight of a single pair (both character are different) = 4 |
| 15 | + * Weight of a single character = 1 |
| 16 | + * <p> |
| 17 | + * This problem was asked during an interview in Adobe. |
| 18 | + * <p> |
| 19 | + * First I provided a DP based solution and then a brute force solution. |
| 20 | + */ |
6 | 21 |
|
7 | | -Weight of string = Weight of total pairs + |
8 | | - weight of single characters - |
9 | | - Total number of toggles. |
| 22 | +package algo.dp; |
10 | 23 |
|
11 | | -Two consecutive characters are considered as pair only if they |
12 | | -are different. |
13 | | -Weight of a single pair (both character are different) = 4 |
14 | | -Weight of a single character = 1 |
| 24 | +public class MaxWeightTransformation { |
15 | 25 |
|
16 | | -This problem was asked during an interview in Adobe. |
17 | 26 |
|
18 | | -First I provided a DP based solution and then a brute force solution. |
19 | | -*/ |
| 27 | + /** |
| 28 | + * Dynamic Programming Solution. |
| 29 | + * str1[] : String array (str.toCharArray()) |
| 30 | + * i: first index of string (0) |
| 31 | + * size: str.length() -1 |
| 32 | + * lookup: int[str.length()] (initialize all lookup indexes with -1) |
| 33 | + */ |
| 34 | + |
| 35 | + public int maxWeightTransformationDPSol(char str1[], int i, int size, int lookup[]) { |
| 36 | + // If the counter has gone past maximum size |
| 37 | + // of string, then return 0 |
| 38 | + if (i > size) |
| 39 | + return 0; |
| 40 | + // If the counter is equal to the size of |
| 41 | + // string, then retun 1. Since, there is no point of string |
| 42 | + // transformation. |
| 43 | + if (i == size) |
| 44 | + return 1; |
| 45 | + |
| 46 | + // if the subproblem is already solved |
| 47 | + if (lookup[i] != -1) |
| 48 | + return lookup[i]; |
| 49 | + |
| 50 | + // If pair is not made, weight gained is 1 |
| 51 | + int res = 1 + maxWeightTransformationDPSol(str1, i + 1, size, lookup); |
| 52 | + |
| 53 | + // is pair is made |
| 54 | + if (i + 1 <= size) { |
| 55 | + // if consecutive elements are same |
| 56 | + // weight gained is 3 after transformation |
| 57 | + if (str1[i] == str1[i + 1]) { |
| 58 | + res = Math.max(res, 3 + maxWeightTransformationDPSol(str1, i + 2, size, lookup)); |
| 59 | + } else if (str1[i] != str1[i + 1]) { // if consecutive elements are different weight gained is 4 |
| 60 | + res = Math.max(res, 4 + maxWeightTransformationDPSol(str1, i + 2, size, lookup)); |
| 61 | + } |
| 62 | + } |
| 63 | + return lookup[i] = res; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Brute Force Solution |
| 68 | + * str1[] : String array (str.toCharArray()) |
| 69 | + * i: first index of string (0) |
| 70 | + * size: str.length() -1 |
| 71 | + */ |
| 72 | + public int maxWeightTransformationNoDPSol(char str1[], int i, int size) { |
| 73 | + // If the counter has gone past maximum size |
| 74 | + // of string, then return 0 |
| 75 | + if (i > size) |
| 76 | + return 0; |
| 77 | + |
| 78 | + // If the counter is equal to the size of |
| 79 | + // string, then retun 1. Since, there is no point of string |
| 80 | + // transformation. |
| 81 | + if (i == size) |
| 82 | + return 1; |
| 83 | + |
| 84 | + // If pair is not made, weight gained is 1 |
| 85 | + int res = 1 + maxWeightTransformationNoDPSol(str1, i + 1, size); |
| 86 | + |
| 87 | + // is pair is made |
| 88 | + if (i + 1 <= size) { |
| 89 | + if (str1[i] == str1[i + 1]) {// if consecutive elements are same, weight gained is 3 after transformation |
| 90 | + res = Math.max(res, 3 + maxWeightTransformationNoDPSol(str1, i + 2, size)); |
| 91 | + } else if (str1[i] != str1[i + 1]) {// if consecutive elements are different, weight gained is 4 |
| 92 | + res = Math.max(res, 4 + maxWeightTransformationNoDPSol(str1, i + 2, size)); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return res; |
| 97 | + } |
20 | 98 |
|
21 | | -package algo.dp; |
22 | 99 |
|
23 | | -public class MaxWeightTransformation{ |
24 | | - |
25 | | - /* |
26 | | - Dynamic Programming Solution. |
27 | | - str1[] : String array (str.toCharArray()) |
28 | | - i: first index of string (0) |
29 | | - size: str.length() -1 |
30 | | - lookup: int[str.length()] (initialize all lookup indexes with -1) |
31 | | - */ |
32 | | - /*public int maxWeightTransformationDPSol(char str1[], int i, int size, int lookup[]) { |
33 | | - // If the counter has gone past maximum size |
34 | | - // of string, then return 0 |
35 | | - if (i > size) |
36 | | - return 0; |
37 | | - // If the counter is equal to the size of |
38 | | - // string, then retun 1. Since, there is no point of string |
39 | | - // transformation. |
40 | | - if (i == size) |
41 | | - return 1; |
42 | | - |
43 | | - // if the subproblem is already solved |
44 | | - if (lookup[i] != -1) |
45 | | - return lookup[i]; |
46 | | - |
47 | | - // If pair is not made, weight gained is 1 |
48 | | - int res = 1 + max_len(str1, i+1, size, lookup); |
49 | | - |
50 | | - // is pair is made |
51 | | - if(i+1 <= size) |
52 | | - { |
53 | | - // if consecutive elements are same |
54 | | - // weight gained is 3 after transformation |
55 | | - if (str1[i] == str1[i+1]) |
56 | | - { |
57 | | - res = Math.max(res, 3 + max_len(str1, i+2, size, lookup)); |
58 | | - } |
59 | | - // if consecutive elements are different |
60 | | - // weight gained is 4 |
61 | | - else if(str1[i] != str1[i+1]) |
62 | | - { |
63 | | - res = Math.max(res, 4 + max_len(str1, i+2, size, lookup)); |
64 | | - } |
65 | | - } |
66 | | - return lookup[i] = res; |
67 | | - }*/ |
68 | | - |
69 | | - /* |
70 | | - Brute Force Solution |
71 | | - str1[] : String array (str.toCharArray()) |
72 | | - i: first index of string (0) |
73 | | - size: str.length() -1 |
74 | | - */ |
75 | | - /*public int maxWeightTransformationNoDPSol(char str1[], int i, int size) { |
76 | | - // If the counter has gone past maximum size |
77 | | - // of string, then return 0 |
78 | | - if (i > size) |
79 | | - return 0; |
80 | | - |
81 | | - // If the counter is equal to the size of |
82 | | - // string, then retun 1. Since, there is no point of string |
83 | | - // transformation. |
84 | | - if (i == size) |
85 | | - return 1; |
86 | | - |
87 | | - // If pair is not made, weight gained is 1 |
88 | | - int res = 1 + max_len(str1, i+1, size); |
89 | | - |
90 | | - // is pair is made |
91 | | - if(i+1 <= size) |
92 | | - { |
93 | | - // if consecutive elements are same |
94 | | - // weight gained is 3 after transformation |
95 | | - if (str1[i] == str1[i+1]) |
96 | | - { |
97 | | - res = Math.max(res, 3 + max_len(str1, i+2, size)); |
98 | | - } |
99 | | - // if consecutive elements are different |
100 | | - // weight gained is 4 |
101 | | - else if(str1[i] != str1[i+1]) |
102 | | - { |
103 | | - res = Math.max(res, 4 + max_len(str1, i+2, size)); |
104 | | - } |
105 | | - } |
106 | | -
|
107 | | - return res; |
108 | | - }*/ |
109 | | - |
110 | | - |
111 | | - } |
| 100 | +} |
112 | 101 |
|
0 commit comments