# Maximize given function by selecting equal length substrings from given Binary Strings

Given two binary strings **s1** and **s2**. The task is to choose substring from **s1** and **s2** say **sub1** and **sub2** of equal length such that it maximizes the function:

fun(s1, s2) = len(sub1) / (2

^{xor(sub1, sub2)})Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the

DSA Self Paced Courseat a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please referComplete Interview Preparation Course.In case you wish to attend

live classeswith experts, please referDSA Live Classes for Working ProfessionalsandCompetitive Programming Live for Students.

**Examples:**

Input:s1= “1101”, s2= “1110”Output:3Explanation:Below are the substrings chosen from s1 and s2

Substring chosen from s1 -> “110”

Substring chosen from s2 -> “110”

Therefore, fun(s1, s2) = 3/ (2^{xor(110, 110)}) = 3, which is maximum possible.

Input:s1= “1111”, s2= “1000”Output:1

**Approach: **In order to maximize the given function large substrings needed to be chosen with minimum XOR. To minimize the denominator, choose substrings in a way such that XOR of **sub1** and **sub2** is always **0** so that the denominator term will always be **1 **(2^{0}). So for that, find the longest common substring from the two strings **s1** and **s2**, and print its length that would be the required answer.

Below is the implementation of above approach:

## C++

`// C++ program for above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `int` `dp[1000][1000];` `// Function to find longest common substring.` `int` `lcs(string s, string k, ` `int` `n, ` `int` `m)` `{` ` ` `for` `(` `int` `i = 0; i <= n; i++) {` ` ` `for` `(` `int` `j = 0; j <= m; j++) {` ` ` `if` `(i == 0 or j == 0) {` ` ` `dp[i][j] = 0;` ` ` `}` ` ` `else` `if` `(s[i - 1] == k[j - 1]) {` ` ` `dp[i][j] = 1 + dp[i - 1][j - 1];` ` ` `}` ` ` `else` `{` ` ` `dp[i][j] = max(dp[i - 1][j],` ` ` `dp[i][j - 1]);` ` ` `}` ` ` `}` ` ` `}` ` ` `// Return the result` ` ` `return` `dp[n][m];` `}` `// Driver Code` `int` `main()` `{` ` ` `string s1 = ` `"1110"` `;` ` ` `string s2 = ` `"1101"` `;` ` ` `cout << lcs(s1, s2,` ` ` `s1.size(), s2.size());` ` ` `return` `0;` `}` |

## Java

`// Java program for above approach` `class` `GFG{` ` ` ` ` `static` `int` `dp[][] = ` `new` `int` `[` `1000` `][` `1000` `];` ` ` `// Function to find longest common substring.` ` ` `static` `int` `lcs(String s, String k, ` `int` `n, ` `int` `m)` ` ` `{` ` ` `for` `(` `int` `i = ` `0` `; i <= n; i++) {` ` ` `for` `(` `int` `j = ` `0` `; j <= m; j++) {` ` ` `if` `(i == ` `0` `|| j == ` `0` `) {` ` ` `dp[i][j] = ` `0` `;` ` ` `}` ` ` `else` `if` `(s.charAt(i - ` `1` `) == k.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 the result` ` ` `return` `dp[n][m];` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String [] args)` ` ` `{` ` ` `String s1 = ` `"1110"` `;` ` ` `String s2 = ` `"1101"` `;` ` ` `System.out.print(lcs(s1, s2,` ` ` `s1.length(), s2.length()));` ` ` ` ` `}` `}` `// This code is contributed by AR_Gaurav` |

## Python3

`# Python3 program for above approach` `import` `numpy as np;` `dp ` `=` `np.zeros((` `1000` `,` `1000` `));` `# Function to find longest common substring.` `def` `lcs( s, k, n, m) :` ` ` `for` `i ` `in` `range` `(n ` `+` `1` `) :` ` ` `for` `j ` `in` `range` `(m ` `+` `1` `) :` ` ` `if` `(i ` `=` `=` `0` `or` `j ` `=` `=` `0` `) :` ` ` `dp[i][j] ` `=` `0` `;` ` ` ` ` `elif` `(s[i ` `-` `1` `] ` `=` `=` `k[j ` `-` `1` `]) :` ` ` `dp[i][j] ` `=` `1` `+` `dp[i ` `-` `1` `][j ` `-` `1` `];` ` ` ` ` `else` `:` ` ` `dp[i][j] ` `=` `max` `(dp[i ` `-` `1` `][j], dp[i][j ` `-` `1` `]);` ` ` `# Return the result` ` ` `return` `dp[n][m];` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `s1 ` `=` `"1110"` `;` ` ` `s2 ` `=` `"1101"` `;` ` ` `print` `(lcs(s1, s2,` `len` `(s1), ` `len` `(s2)));` ` ` `# This code is contributed by AnkThon` |

## C#

`// C# program for above approach` `using` `System;` `public` `class` `GFG{` ` ` ` ` `static` `int` `[,]dp = ` `new` `int` `[1000,1000];` ` ` `// Function to find longest common substring.` ` ` `static` `int` `lcs(` `string` `s, ` `string` `k, ` `int` `n, ` `int` `m)` ` ` `{` ` ` `for` `(` `int` `i = 0; i <= n; i++) {` ` ` `for` `(` `int` `j = 0; j <= m; j++) {` ` ` `if` `(i == 0 || j == 0) {` ` ` `dp[i, j] = 0;` ` ` `}` ` ` `else` `if` `(s[i - 1] == k[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 the result` ` ` `return` `dp[n, m];` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main(` `string` `[] args)` ` ` `{` ` ` `string` `s1 = ` `"1110"` `;` ` ` `string` `s2 = ` `"1101"` `;` ` ` `Console.Write(lcs(s1, s2, s1.Length, s2.Length));` ` ` `}` `}` `// This code is contributed by AnkThon` |

## Javascript

`<script>` `// JavaScript program for above approach` `var` `dp = ` `new` `Array(1000);` `for` `(` `var` `i = 0; i < 1000; i++) {` ` ` `dp[i] = ` `new` `Array(1000);` `}` ` ` `// Function to find longest common substring.` `function` `lcs( s, k, n, m)` `{` ` ` `for` `(` `var` `i = 0; i <= n; i++) {` ` ` `for` `(` `var` `j = 0; j <= m; j++) {` ` ` `if` `(i == 0 || j == 0) {` ` ` `dp[i][j] = 0;` ` ` `}` ` ` `else` `if` `(s[i - 1] == k[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 the result` ` ` `return` `dp[n][m];` `}` `// Driver Code` `var` `s1 = ` `"1110"` `;` `var` `s2 = ` `"1101"` `;` `document.write(lcs(s1, s2, s1.length, s2.length))` `// This code is contributed by AnkThon` `</script>` |

**Output**

3

**Time Complexity: **O(N*M), where N is the size of s1 and M is the size of s2.

**Auxiliary Space:** O(N*M), where N is the size of s1 and M is the size of s2.