Blog Archive

Minimum Changes To Make Alternating Binary String


You are given a string s consisting only of the characters '0' and '1'. In one operation, you can change any '0' to '1' or vice versa.

The string is called alternating if no two adjacent characters are equal. For example, the string "010" is alternating, while the string "0100" is not.

Return the minimum number of operations needed to make s alternating

Example :

Input: s = "0100"
Output: 1

Explanation: If you change the last character to '1', s will be "0101", which is alternating 

SOLUTION

int minOperations(string s)
{
    int n = s.length();

    if (n <= 1)
        return 0;

    int c1 = 0;
    int c0 = 0;
    for (int i = 0i < ni++)
    {
        if (i % 2)
        {
            if (s[i] == '1')
                c0++;
            else
                c1++;
        }
        else
        {
            if (s[i] == '0')
                c0++;
            else
                c1++;
        }
    }

    return min(c0c1);
}
Data structures and algorithm - String Data Structure


Thank You...

Next Permutation

Next Permutation



Implement the next permutation, which rearranges the list of numbers into Lexicographically next greater permutation of list of numbers. If such arrangement is not possible, it must be rearranged to the lowest possible order i.e. sorted in an ascending order. You are given an list of numbers arr[ ] of size N

Example :

Input: N = 3
arr = {3, 2, 1}
Output: {1, 2, 3}
Explaination: As arr[] is the last permutation. 
So, the next permutation is the lowest one.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:

1 ≤ N ≤ 100  

SOLUTION

vector<intnextPermutation(int nvector<intnums)
{
    // code here
    int idx = -1;
    for (int i = n - 1i > 0i--)
    {
        if (nums[i] > nums[i - 1])
        {
            idx = i;
            break;
        }
    }
    if (idx == -1)
    {
        reverse(nums.begin(), nums.end());
    }
    else
    {
        int prev = idx;
        for (int i = idx + 1i < ni++)
        {
            if (nums[i] > nums[idx - 1] && nums[i] <= nums[prev])
            {
                prev = i;
            }
        }
        swap(nums[idx - 1], nums[prev]);
        reverse(nums.begin() + idxnums.end());
    }
    return nums;
}

Data Structures & Algorithms, String and Array Data Structure


THANK  YOU...Explore Other Programmes Also

Split the binary string into substrings with equal number of 0s and 1s

 Split the binary string into substrings with equal number of 0s and 1s

Q.Given a binary string str of length N, the task is to find the maximum count of substrings str can be divided into such that all the substrings are balanced i.e. they have equal number of 0s and 1s. If it is not possible to split str satisfying the conditions then print -1

Example:

Input: str = “0100110101”
Output: 4
The required substrings are “01”, “0011”, “01” and “01”.

Input: str = “0111100010”
Output: 3


SOLUTION

#include <iostream >
    using namespace std;
int countBinary(string sint n){
    int count0 = 0;
    int count1 = 0;
    int count = 0;
    for (int i = 0i < ni++) {
        if (s[i] == '0') {
            count0++;
        }
        else {
            count1++;
        }
        if (count0 == count1) {
            count++;
        }
    }
    if (count0 != count1) {
        return -1;
    }

    return count;
}
int main() {
    string s;
    cin >> s;
    int n = s.length();
    int num = countBinary(sn);
    cout << num;
    return 0;
}

Data Structures and Algorithms


Thank You 

Permutations of a given string

 Permutations of a given string 

Q - Permutations of a given string

Given a string S. The task is to print all permutations of a given string.

Input:
The first line of input contains an integer T, denoting the number of test cases. Each test case contains a single string in capital letter.

Output:
For each test case, print all permutations of a given string with single space and all permutations should be in lexicographically increasing order.

Constraints:
1 ≤ T ≤ 10
1 ≤ size of string ≤ 5

Example:
Input:
2
ABC
ABSG

Output:
ABC ACB BAC BCA CAB CBA 
ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA

Explanation:
Testcase 1:
 Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA .


SOLUTION

#include < iostream >
    using namespace std;

void permutation(string sint i){
    //base Case
    if (s[i] == '\0') {
        cout << s << " ";
        return;
    }
    //recursive case
    for (int j = is[j] != '\0'j++) {
        swap(s[i], s[j]);
        permutation(si + 1);
        swap(s[i], s[j]);
    }
}
int main()
{
    //code
    int tcin >> t;
    while (t--) {

        string S;
        cin >> S;
        permutation(S0);
        cout << endl;
    }

    return 0;
}


Data Structures and Algorithm-String Data Structure


Recent Post

Sort an array of 0s, 1s and 2s

  Sort an array of 0s, 1s and 2s- Data Structures   Sort an array of 0s, 1s and 2s- Data Structures Given an array of size N containing onl...