-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBetter String.java
More file actions
69 lines (55 loc) · 2.14 KB
/
Better String.java
File metadata and controls
69 lines (55 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//{ Driver Code Starts
// Initial Template for Java
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
class GFG {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
String str1 = sc.next();
String str2 = sc.next();
Solution obj = new Solution();
String ans = obj.betterString(str1, str2);
System.out.println(ans);
}
}
}
// } Driver Code Ends
// User function Template for Java
class Solution {
public static String betterString(String str1, String str2) {
//
// Count distinct subsequences for each string
int count1 = countDistinctSubsequences(str1);
int count2 = countDistinctSubsequences(str2);
// Compare counts and return the better string
return (count1 >= count2) ? str1 : str2;
}
// Method to count distinct subsequences using dynamic programming
private static int countDistinctSubsequences(String str) {
int MOD = 1000000007;
int n = str.length();
// Array to store the last occurrence index of each character
int[] lastOccurrence = new int[256];
Arrays.fill(lastOccurrence, -1);
// Dynamic programming array to store count of distinct subsequences
int[] dp = new int[n + 1];
dp[0] = 1; // Base case: empty string has one subsequence
// Iterate through each character of the string
for (int i = 1; i <= n; i++) {
// Calculate count based on the recurrence relation
dp[i] = (2 * dp[i - 1]) % MOD;
// Subtract count of subsequences ending at the last occurrence of the current character
if (lastOccurrence[str.charAt(i - 1)] != -1) {
dp[i] = (dp[i] - dp[lastOccurrence[str.charAt(i - 1)] - 1] + MOD) % MOD;
}
// Update last occurrence index of the current character
lastOccurrence[str.charAt(i - 1)] = i;
}
// Return the final count of distinct subsequences
return dp[n];
}
}