Blog Archive

Showing posts with label data structures and algorithms. Show all posts
Showing posts with label data structures and algorithms. Show all posts

Convert a sentence into its equivalent mobile numeric keypad sequence

 Convert a sentence into its equivalent mobile numeric keypad sequence-Data Structure and Algorithms

Given a sentence in the form of a string, convert it into its equivalent mobile numeric keypad sequence


Input : STRIVE
Output : 7777877744488833
For obtaining a number, we need to press a
number corresponding to that character for 
number of times equal to position of the 
character. For example, for character C, 
we press number 2 three times and accordingly.

Input : HELLO WORLD
Output : 4433555555666096667775553

Solution:

// #include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    // cout<<"Hello World!";
    string s[] = {"2""22""222",
                  "3""33""333",
                  "4""44""444",
                  "5""55""555",
                  "6""66""666",
                  "7""77""777""7777",
                  "8""88""888",
                  "9""99""999""9999"};
    string input;
    cin >> input;
    string ans = "";
    for (int i = 0i < input.length(); i++)
    {
        if (input[i] == ' ')
            ans += '0';
        else
        {
            int idx = input[i] - 'A';
            ans += s[idx];
        }
    }
    cout << ans << endl;
    return 0;
}

Thank You

data structures and algorithm-String data structure

Longest Prefix Suffix (KMP Algorithm)

 Longest Prefix Suffix (KMP Algo)

Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.

Example:

Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper 
prefix and suffix.

Steps:

Make a array to store length of the matching prefix and Suffix  in order that the last entry will have the matched prefix and suffix length.




Solution:

int lps(string s)
{
    vector<intA(s.size(), 0);
    int j = 0i = 1;

    while (i < s.size())
    {
        if (s[i] == s[j])
        {
            A[i] = j + 1;
            j++;
            i++;
        }
        else
        {
            if (j == 0)
                i++;
            else
                j = A[j - 1];
        }
    }

    return A.back();
    // back() return last entry of the vector
}

data structures and algorithm- string data structure


Thank You.

Parenthesis Checker

 Parenthesis Checker

Given an expression string x. Examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[“,”]” are correct in exp.
For example, the function should return 'true' for exp = “[()]{}{[()()]()}” and 'false' for exp = “[(])”

Example 1:

Input:
{([])}
Output: 
true
Explanation: 
{ ( [ ] ) }. Same colored brackets can form 
balaced pairs, with 0 number of 
unbalanced bracket.

Example 2:

Input: 
()
Output: 
true
Explanation: 
(). Same bracket can form balanced pairs, 
and here only 1 type of bracket is 
present and in balanced way

Solution:

bool ispar(string x)
{
    // Your code here
    stack<char> s;
    for (int i = 0i < x.size(); i++)
    {
        if (x[i] == '(' || x[i] == '{' || x[i] == '[')
            s.push(x[i]);
        else
        {
            if (s.empty())
                return false;

            char c = s.top();
            if (x[i] == ')' && c != '(')
                return false;
            else if (x[i] == '}' && c != '{')
                return false;
            else if (x[i] == ']' && c != '[')
                return false;
            s.pop();
        }
    }
    return s.empty();
}

data structures and algorithms


Count Number of Homogenous Substrings

Count Number of Homogenous Substrings

 Count Number of Homogenous Substrings- Data Structures and Algorithms-Leetcode Weekly Challenge


Given a string s, return the number of homogenous substrings of s. Since the answer may be too large, return it modulo 109 + 7.

A string is homogenous if all the characters of the string are the same.

substring is a contiguous sequence of characters within a string

Example:

Input: s = "abbcccaa"
Output: 13
Explanation: The homogenous substrings are listed as below:
"a"   appears 3 times.
"aa"  appears 1 time.
"b"   appears 2 times.
"bb"  appears 1 time.
"c"   appears 3 times.
"cc"  appears 2 times.
"ccc" appears 1 time.
3 + 1 + 2 + 1 + 3 + 2 + 1 = 13.
SOLUTION

int countHomogenous(string s)
{
    long long int num = 1e9 + 7;
    long long int result = 0;
    long long int count = 1;
    for (int i = 1i < s.size(); i++)
    {
        if (s[i] == s[i - 1])
        {
            count++;
        }
        else
        {
            result += (count * (count + 1)) / 2;
            count = 1;
        }
    }
    result += (count * (count + 1)) / 2;
    result = result % num;
    return result;
}

Data Structures and Algorithms-Leetcode Weekly Challenge


Thank You...

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...

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...