# String Construction - Google Top Interview Questions

### Problem Statement :

```You are given a list of strings strings where each string contains "A"s and "B"s.

You are also given integers a and b.

Return the maximum number of strings that can be constructed given that you can use at most a "A"s and at most b "B"s, without reuse.

Constraints

n ≤ 50 where n is the length of strings

a, b ≤ 50

Example 1

Input

strings = ["AABB", "AAAB", "A", "B"]

a = 4

b = 2

Output

3

Explanation

We can take these strings using 4 "A"s and 2 "B"s ["AAAB","A","B"]```

### Solution :

```                        ```Solution in C++ :

int dp;

int solve(vector<string>& strings, int a, int b) {
int N = strings.size();
vector<pair<int, int>> pairs;
pairs.reserve(N);
for (const string& s : strings) {
int num_a = count(s.begin(), s.end(), 'A');
int num_b = s.size() - num_a;
pairs.emplace_back(num_a, num_b);
}
for (int k = 1; k <= N; k++) {
auto p = pairs[k - 1];
for (int i = 0; i <= a; i++) {
for (int j = 0; j <= b; j++) {
int t = dp[k - 1][i][j];  // don't use the current string
if (i >= p.first && j >= p.second) {
// do use the current string
t = max(t, 1 + dp[k - 1][i - p.first][j - p.second]);
}
dp[k][i][j] = t;
}
}
}
return dp[N][a][b];
}```
```

```                        ```Solution in Java :

import java.util.*;

class Solution {
private static final int len = 52;
public int solve(String[] arr, int a, int b) {
int[][][] dp = new int[len][len][len];

for (int i = 1; i <= arr.length; i++) {
String str = arr[i - 1];
int countA = getCount(str, 'A');
int countB = getCount(str, 'B');
for (int j = 0; j <= a; j++) {
for (int k = 0; k <= b; k++) {
dp[i][j][k] = dp[i - 1][j][k];
if (j - countA >= 0 && k - countB >= 0)
dp[i][j][k] =
Math.max(dp[i - 1][j][k], 1 + dp[i - 1][j - countA][k - countB]);
}
}
}
return dp[arr.length][a][b];
}

private int getCount(String str, char chr) {
int count = 0;
for (char ch : str.toCharArray()) {
if (ch == chr)
++count;
}
return count;
}
}```
```

```                        ```Solution in Python :

class Solution:
def solve(self, strings, a, b):
dp = defaultdict(int, {(a, b): 0})
ans = 0
for s in strings:
f = Counter(s)
a_count = f["A"]
b_count = f["B"]
for (i, j), val in list(dp.items()):
if i >= a_count and j >= b_count:
dp[(i - a_count, j - b_count)] = max(dp[(i - a_count, j - b_count)], val + 1)
ans = max(ans, dp[(i - a_count, j - b_count)])
return ans```
```

## View More Similar Problems

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

## Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

## Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

## Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

## Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer