Mar 8, 2022

Rotate matrix in place

 #include <iostream>

using namespace std;


void printMatrix(int a[4][4])

{

    for(int i=0;i<4;i++)

    {

        for(int j=0;j<4;j++)

        {

            cout << a[i][j] << " ";

        }

        cout << endl;

    }

}

void rotateMatrixInPlace(int a[4][4])

{

    cout << "before rotation\n";

    printMatrix(a);

    

// Transpose 

    for(int i=0;i<4; i++)

    {

        for(int j=i+1;j<4;j++)

        {

            int t = a[i][j];

            a[i][j] = a[j][i];

            a[j][i] = t;

        }

        

    }


// Mirror    

    for(int i=0;i<4; i++)

    {

        for(int j=0;j<2;j++)

        {

            int t = a[i][j];

            a[i][j] = a[i][3-j];

            a[i][3-j] = t;

        }

    }

    

    cout << "after rotation\n";

    printMatrix(a);

}

int main() {

// your code goes here

int a[4][4] = 

{

    1, 2, 3, 4,

    5, 6, 7, 8,

    9, 0, 1, 2,

    3, 4, 5, 6

};

rotateMatrixInPlace(a);

return 0;

}


one away string

 #include <iostream>

#include <cstring>


using namespace std;


const int maxsize = 128;


void oneAway(char a[], char b[])

{

    int dist = strlen(a) - strlen(b);

    if(dist  > 1 || dist  < -1 )

    {

        cout << "false";

        return;

    }

    

    

    

    if(dist == 0)

    {

        //check for 1 replace

        int p=0;

        for(int i=0; i< strlen(a); i++)

        {

            if(a[i] != b[i])

            {

                ++p;

                if(p>1)

                {

                    cout << "false";

                    return;

                }

            }

        }

    }

    else

    {

        //check for 1 insert or remove :  (ab, a | b), (b, ab | ba)

        int p=0;

        int index = strlen(a) > strlen(b) ? 1 : 0;

        int len =  (index == 1) ? strlen(a) : strlen(b);

        int change=0;

        for(int i=0, j=0; i<len; i++, j++)

        {

            if(a[i] != b[j])

            {

                if(index == 1)

                {

                    i++;

                    change++;

                }

                else

                {

                    j++;

                    change++;

                }

            }

            

            if(change >1)

            {

                cout << false;

                return;

            }

            

        }

    }

    

    cout << "true";

}


int main() {

// your code goes here

char *a = "pale";

char *b = "pale";

oneAway(a, b);

return 0;

}


Permutation palindrome

#include <iostream>
#include <cstring>
using namespace std;

const int maxsize = 128;

void is_permutationPalindrome(char s[])
{
    char r[maxsize];
    memset(r, 0, maxsize);

    for(int i=0; i<strlen(s); i++)
    {
        r[s[i]] ++;
    }
    
    int num_evens = 0, num_odds = 0;
    
    for(int i=0; i<maxsize; i++)
    {
        if(r[i] %2 == 0)
            num_evens++;
        else
            num_odds++;
    }
    
    if(num_odds == 1 || num_odds == 0)
        cout << "it is a palindrome permutation";
    else
        cout << "it is not a palindrome permutation";
}

int main() {
// your code goes here
char *s = "defer";
is_permutationPalindrome(s);
return 0;
}