-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
47bc047
commit 7fe0f7c
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//{ Driver Code Starts | ||
// Initial Template for Java | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class GfG { | ||
public static void main(String args[]) { | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
while (t-- > 0) { | ||
String X = sc.next(); | ||
String Y = sc.next(); | ||
int costX = sc.nextInt(); | ||
int costY = sc.nextInt(); | ||
|
||
Solution ob = new Solution(); | ||
System.out.println(ob.findMinCost(X, Y, costX, costY)); | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
class Solution | ||
{ | ||
public int findMinCost(String x, String y, int costX, int costY) | ||
{ | ||
int n = x.length(), m = y.length(); | ||
int dp[][] = new int[n+1][m+1]; | ||
|
||
for (int i=0; i<=n; i++) | ||
dp[i][0] = 0; | ||
|
||
for (int i=0; i<=m; i++) | ||
dp[0][i] = 0; | ||
|
||
for (int i=1; i<=n; i++) | ||
{ | ||
for (int j=1; j<=m; j++) | ||
{ | ||
if (x.charAt(i-1) == y.charAt(j-1)) | ||
dp[i][j] = 1 + dp[i-1][j-1]; | ||
else | ||
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]); | ||
} | ||
} | ||
return (n-dp[n][m])*costX + (m-dp[n][m])*costY; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n*m) | ||
Space complexity - O(n*m) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class Solution | ||
{ | ||
public int checkRecord(int n) | ||
{ | ||
int kMod = 1_000_000_007; | ||
// dp[i][j] := the length so far with i A's and the last letters are j L's | ||
long[][] dp = new long[2][3]; | ||
dp[0][0] = 1; | ||
|
||
while (n-- > 0) | ||
{ | ||
long[][] prev = Arrays.stream(dp).map((long[] A) -> A.clone()) | ||
.toArray((int length) -> new long[length][]); | ||
|
||
// Append a P. | ||
dp[0][0] = (prev[0][0] + prev[0][1] + prev[0][2]) % kMod; | ||
|
||
// Append an L. | ||
dp[0][1] = prev[0][0]; | ||
|
||
// Append an L. | ||
dp[0][2] = prev[0][1]; | ||
|
||
// Append an A or append a P. | ||
dp[1][0] = (prev[0][0] + prev[0][1] + prev[0][2] + prev[1][0] + prev[1][1] + prev[1][2]) % kMod; | ||
|
||
// Append an L. | ||
dp[1][1] = prev[1][0]; | ||
|
||
// Append an L. | ||
dp[1][2] = prev[1][1]; | ||
} | ||
|
||
return (int) ((dp[0][0] + dp[0][1] + dp[0][2] + dp[1][0] + dp[1][1] + dp[1][2]) % kMod); | ||
} | ||
} |