 1598058000

# Count of distinct Strings possible by swapping prefixes of pairs of Strings

Given an array** string[]** consisting of N numeric strings of length M, the task is to find the number of distinct strings that can be generated by selecting any two strings, say i and j from the array and swap all possible prefixes between them.

_Note: _Since the answer can be very large, print modulo 1000000007.

Examples:

Input:_ N = 2 M = 3 string[] = {“112”, “211”}_

Output:_ 4_

Explanation:

Swapping “1” and “2” between the strings generates “212” and “111“.

Swapping “11” and “21” between the strings generates “212” and “111”.

Swapping “112” and “211” between the strings generates “211” and “112“.

Therefore, 4 distinct strings are generated.

_Input: _N = 4 M = 5 string[] = {“12121”, “23545”, “11111”, “71261”}

_Output: _216

### Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Considering a string s of the form s1s2s3s4…sm, where s1 is the first letter of any of the strings in the array, s2 is the second letter of any of the strings, and so on, the answer to the problem is the product of count(i) where **count(i) **is the count of different letters placed at same index in the given strings.

Below is the implementation of the above approach:

• Java

`// Java Program to implement`

`// the above approach`

`**import**` `java.util.*;`

`**import**` `java.io.*;`

`**public**` `**class**` `Main {`

`**static**` `**int**` `mod =` `1000000007``;`

`// Function to count the distinct strings`

`// possible after swapping the prefixes`

`// between two possible strings of the array`

`**public**` `**static**` `**long**` `countS(String str[],` `**int**` `n,` `**int**` `m)`

`{`

`// Stores the count of unique`

`// characters for each index`

`Map<Integer, Set<Character> > counts`

`=` `**new**` `HashMap<>();`

`**for**` `(``**int**` `i =` `0``; i < m; i++) {`

`counts.put(i,` `**new**` `HashSet<>());`

`}`

`**for**` `(``**int**` `i =` `0``; i < n; i++) {`

`// Store current string`

`String s = str[i];`

`**for**` `(``**int**` `j =` `0``; j < m; j++) {`

`counts.get(j).add(s.charAt(j));`

`}`

`}`

`// Stores the total number of`

`// distinct strings possible`

`**long**` `result =` `1``;`

`**for**` `(``**int**` `index : counts.keySet())`

`result = (result`

`* counts.get(index).size())`

`% mod;`

`// Return the answer`

`**return**` `result;`

`}`

`// Driver Code`

`**public**` `**static**` `**void**` `main(String[] args)`

`{`

`String str[] = {` `"112"``,` `"211"` `};`

`**int**` `N =` `2``, M =` `3``;`

`System.out.println(countS(str, N, M));`

`}`

`}`

Output:

``````4
``````

Time Complexity:_ O(N * M)_

Auxiliary Space:_ O(N)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #hash #mathematical #searching #strings #frequency-counting

## Buddha Community  1598058000

## Count of distinct Strings possible by swapping prefixes of pairs of Strings

Given an array** string[]** consisting of N numeric strings of length M, the task is to find the number of distinct strings that can be generated by selecting any two strings, say i and j from the array and swap all possible prefixes between them.

_Note: _Since the answer can be very large, print modulo 1000000007.

Examples:

Input:_ N = 2 M = 3 string[] = {“112”, “211”}_

Output:_ 4_

Explanation:

Swapping “1” and “2” between the strings generates “212” and “111“.

Swapping “11” and “21” between the strings generates “212” and “111”.

Swapping “112” and “211” between the strings generates “211” and “112“.

Therefore, 4 distinct strings are generated.

_Input: _N = 4 M = 5 string[] = {“12121”, “23545”, “11111”, “71261”}

_Output: _216

### Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Considering a string s of the form s1s2s3s4…sm, where s1 is the first letter of any of the strings in the array, s2 is the second letter of any of the strings, and so on, the answer to the problem is the product of count(i) where **count(i) **is the count of different letters placed at same index in the given strings.

Below is the implementation of the above approach:

• Java

`// Java Program to implement`

`// the above approach`

`**import**` `java.util.*;`

`**import**` `java.io.*;`

`**public**` `**class**` `Main {`

`**static**` `**int**` `mod =` `1000000007``;`

`// Function to count the distinct strings`

`// possible after swapping the prefixes`

`// between two possible strings of the array`

`**public**` `**static**` `**long**` `countS(String str[],` `**int**` `n,` `**int**` `m)`

`{`

`// Stores the count of unique`

`// characters for each index`

`Map<Integer, Set<Character> > counts`

`=` `**new**` `HashMap<>();`

`**for**` `(``**int**` `i =` `0``; i < m; i++) {`

`counts.put(i,` `**new**` `HashSet<>());`

`}`

`**for**` `(``**int**` `i =` `0``; i < n; i++) {`

`// Store current string`

`String s = str[i];`

`**for**` `(``**int**` `j =` `0``; j < m; j++) {`

`counts.get(j).add(s.charAt(j));`

`}`

`}`

`// Stores the total number of`

`// distinct strings possible`

`**long**` `result =` `1``;`

`**for**` `(``**int**` `index : counts.keySet())`

`result = (result`

`* counts.get(index).size())`

`% mod;`

`// Return the answer`

`**return**` `result;`

`}`

`// Driver Code`

`**public**` `**static**` `**void**` `main(String[] args)`

`{`

`String str[] = {` `"112"``,` `"211"` `};`

`**int**` `N =` `2``, M =` `3``;`

`System.out.println(countS(str, N, M));`

`}`

`}`

Output:

``````4
``````

Time Complexity:_ O(N * M)_

Auxiliary Space:_ O(N)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #hash #mathematical #searching #strings #frequency-counting 1597059480

## Queries to count distinct Binary Strings of all lengths

Given an K and a matrix Q[][] consisting of queries of the form {N, M}, the task for each query is to count the number of strings possible of all lengths from Q[i] to Q[i] satisfying the following properties:

• Frequency of 0‘s are equal to a multiple of K.
• Two strings are said to be different only if the frequencies of 0‘s and 1‘s are different

Since the answer can be quite large, compute the answer by mod 109 + 7.

Examples:

Input_: K = 3, Q[][] = {{1, 3}} _

Output_: 4 _

Explanation:

_All possible strings of length 1 : {“1”} _

_All possible strings of length 2 : {“11”} _

_All possible strings of length 3 : {“111”, “000”} _

Therefore, a total of 4 strings can be generated.

Input_: K = 3, Q[][] = {{1, 4}, {3, 7}} _

_Output: _

_7 _

_24 _

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach:

Follow the steps below to solve the problem:

• Initialize an array dp[] such that dp[i] denote the number of strings possible of length i.
• Initialize dp = 1.
• For every ith length, at most two possiblities arise:
• Appending ‘1’ to the strings of length i – 1.
• Add K 0‘s to all possible strings of length i-K.
• Finally, for each query Q[i], print the sum of all **dp[j] **for Q[i] <= j <= Q[i].

Time Complexity:_ O(N*Q) _

Auxiliary Space:_ O(N)_

Efficient Approach:

The above approach can be optimized using Prefix Sum Array. Follow the steps below:

• Update the dp[] array by following the steps in the above approach.
• Compute prefix sum array of the dp[] array.
• Finally, for each query** Q[i]**, calculate dp[Q[i]] – dp[Q[i] – 1] and print as result.

Below is the implementation of the above approach:

• C++
• Java
• Python3

`// C++ Program to implement`

`// the above approach`

`#include <bits/stdc++.h>`

`**using**` `**namespace**` `std;`

`**const**` `**int**` `N = 1e5 + 5;`

`**const**` `**int**` `MOD = 1000000007;`

`**long**` `**int**` `dp[N];`

`// Function to calculate the`

`// count of possible strings`

`**void**` `countStrings(``**int**` `K,`

`vector<vector<``**int**``> > Q)`

`{`

`// Initialize dp`

`dp = 1;`

`// dp[i] represents count of`

`// strings of length i`

`**for**` `(``**int**` `i = 1; i < N; i++) {`

`dp[i] = dp[i - 1];`

`// Add dp[i-k] if i>=k`

`**if**` `(i >= K)`

`dp[i]`

`= (dp[i] + dp[i - K]) % MOD;`

`}`

`// Update Prefix Sum Array`

`**for**` `(``**int**` `i = 1; i < N; i++) {`

`dp[i] = (dp[i] + dp[i - 1]) % MOD;`

`}`

`**for**` `(``**int**` `i = 0; i < Q.size(); i++) {`

`**long**` `**int**` `ans`

`= dp[Q[i]] - dp[Q[i] - 1];`

`**if**` `(ans < 0)`

`ans = ans + MOD;`

`cout << ans << endl;`

`}`

`}`

`// Driver Code`

`**int**` `main()`

`{`

`**int**` `K = 3;`

`vector<vector<``**int**``> > Q`

`= { { 1, 4 }, { 3, 7 } };`

`countStrings(K, Q);`

`**return**` `0;`

`}`

Output:

``````7
24
``````

Time Complexity:_ O(N + Q) _

_Auxiliary Space: _O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#dynamic programming #mathematical #strings #binary-string #prefix-sum 1619682840

## What is difference between String and string in C#

Are you confused about System.String and string in C#? What is the difference between String and string in C#? And how to choose between string and System.String? In this article, I am going to show you all the differences between string and System.String in C## with code examples. .

### What is the difference between System.String and string in C#?

Basically, there is no difference between string and String in C#. “string” is just an alias of System.String and both are compiled in the same manner. String stands for System.String and it is a .NET Framework type. “string” is an alias in the C## language for System.String. Both of them are compiled to System.String in IL (Intermediate Language), so there is no difference.

#c# #string #string and string 1597470600

## Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's

Given an integer N, the task is to find the number of Binary Strings of length N such that frequency of 1‘s is greater than the frequency of 0‘s.

Example:

Input:_ N = 2_

Output:_ 1_

_Explanation: __Count of binary strings of length 2 is 4 i.e. {“00”, “01”, “10”, “11”}. _

The only string having frequency of 1’s greater than that of 0’s is “11”.

Input:_ N = 3_

Output:_ 4_

_Explanation: _Count of binary strings of length 3 is 8 i.e. {“000”, “001”, “010”, “011”, “100”, “101”, “110”, “111”}.

Among them, the strings having frequency of 1’s greater than 0’s are {“011”, “101”, “110”, “111”}.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: The simplest approach to solve this problem is to generate all binary strings of length N, and iterate over each string to find the frequency of 1’s and 0’s. If the frequency of 1’s is greater than that of 0’s, increment the counter. Finally, print the count.

Time Complexity:_ O(N*2N) _

Auxilairy Space:_ O(1)_

Efficient Approach: To observe the above approach, following observations need to made:

_STotal __= Total Number of binary strings of length N = 2N _

_Sequal __= Number of binary string of length N having same frequency of 0’s and 1’s. _

S1_ = Number of binary strings of length N having frequency of 1’s greater than 0’s. _

S0_ = Number of binary strings of length N having frequency of 0’s greater than 1’s. _

Stotal = Sequal + S1 + S0

1. For every string in S1, there exist a string in S0.
2. Suppose “1110” is the string in S1 then its corresponding string in S0  will be “0001“. Similarly, for every string in S1 there exist a string in S0. Hence, S1 = S0 ( for every N).
3. If N is odd then** Sequal = 0**.
4. If N is even then Sequal =C(N, N/2).

Below is the implementation of the above approach:

• C++
• Python3

`// C++ Program to implement`

`// the above approach`

`#include <bits/stdc++.h>`

`**using**` `**namespace**` `std;`

`// Function to calculate`

`// and return the value of`

`// Binomial Coefficient C(n, k)`

`unsigned` `**long**` `**int**` `binomialCoeff(unsigned` `**long**` `**int**` `n,`

`unsigned` `**long**` `**int**` `k)`

`{`

`unsigned` `**long**` `**int**` `res = 1;`

`// Since C(n, k) = C(n, n-k)`

`**if**` `(k > n - k)`

`k = n - k;`

`// Calculate the value of`

`// [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1]`

`**for**` `(``**int**` `i = 0; i < k; ++i) {`

`res *= (n - i);`

`res /= (i + 1);`

`}`

`**return**` `res;`

`}`

`// Function to return the count of`

`// binary strings of length N such`

`// that frequency of 1's exceed that of 0's`

`unsigned` `**long**` `**int**` `countOfString(``**int**` `N)`

`{`

`// Count of N-length binary strings`

`unsigned` `**long**` `**int**` `Stotal =` `**pow**``(2, N);`

`// Count of N- length binary strings`

`// having equal count of 0's and 1's`

`unsigned` `**long**` `**int**` `Sequal = 0;`

`// For even length strings`

`**if**` `(N % 2 == 0)`

`Sequal = binomialCoeff(N, N / 2);`

`unsigned` `**long**` `**int**` `S1 = (Stotal - Sequal) / 2;`

`**return**` `S1;`

`}`

`// Driver Code`

`**int**` `main()`

`{`

`**int**` `N = 3;`

`cout << countOfString(N);`

`**return**` `0;`

`}`

Output:

``````4
``````

Time Complexity:_ O(N)_

Auxiliary Space:_ O(1)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#bit magic #combinatorial #mathematical #strings #binary-string #frequency-counting #setbitcount 1601154060

## How to Check String or String View Prefixes and Suffixes in C++20

Up to (and including) C++17 if you wanted to check the start or the end in a string you have to use custom solutions, boost or other third-party libraries. Fortunately, this changes with C++20.

See the article where I’ll show you the new functionalities and discuss a couple of examples.

### Intro

Here’s the main proposal that was added into C++20:

C++

``````std::string/std::string_view .starts_with() and .ends_with() P0457
``````

In the new C++ Standard, we’ll get the following member functions for `std::string` and `std::string_view`:

C++

``````constexpr bool starts_with(string_view sv) const noexcept;

constexpr bool starts_with(CharT c ) const noexcept;

constexpr bool starts_with(const CharT* s ) const;
``````

And also for suffix checking:

C++

``````constexpr bool ends_with(string_view sv )const noexcept;

constexpr bool ends_with(CharT c ) const noexcept;

constexpr bool ends_with(const CharT* s ) const;
``````

As you can see, they have three overloads: for a `string_view`, a single character and a string literal.

Simple example:

C++

``````const std::string url { "https://isocpp.org" };

// string literals

if (url.starts_with("https") && url.ends_with(".org"))

std::cout << "you're using the correct site!\n";

// a single char:

if (url.starts_with('h') && url.ends_with('g'))

std::cout << "letters matched!\n";
``````

You can play with this basic example @Wandbox

#tutorial #iot #c++ #visual c++ #vc++ #c++20 #string view prefixes #string view suffixes