Illegible String

Your friend Max has written a string S in your textbook. The string consists of lowercase Latin letters. The problem is that Max is not good at writing at all! Especially, you never know if he wanted to write "w" or two consecutive "v". Given the string S, return the minimum and maximum length of a word which can be represented by it. The input string represents what you initially think the word is.

Example:

Input:  n = 5, s = "avwvb"
Output: 4 6

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void illegibleString(int nstring s)
{
    int cntw = 0, cntv = 0;
    string str = "";
    for (int i = 0; i < n; i++)
    {
        if (s[i] == 'w')
        {
            str += 'v';
            str += 'v';
        }
        else
            str += s[i];
    }
    string res = "";
    int i = 0;
    while (i < str.size())
    {
        if (i < str.size() - 1 &&
            str[i] == 'v' && str[i + 1] == 'v')
        {
            res += 'w';
            i += 2;
        }
        else
        {
            res += str[i];
            i++;
        }
    }
    cout << res.size() << " " << str.size() << "\n";
}
int main()
{
    int n = 5;

    string s = "avwvb";

    illegibleString(n, s);

    return 0;
}


No comments:

Post a Comment