Make The String Great

Given a string s of lower and upper case English letters.

A good string is a string that doesn't have two adjacent characters s[i] and s[i + 1] where:

  • 0 <= i <= s.length - 2
  • s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.

To make the string good, you can choose two adjacent characters that make the string bad and remove them. You can keep doing this until the string becomes good.

Return the string after making it good. The answer is guaranteed to be unique under the given constraints.

Notice that an empty string is also good.

Example:

Input:  str="aDdCD"
Output: "aCD"

Approach

Java

public class MakeTheStringGreat {
    public static void main(String[] args) {
        String str = "aDdCD";
        System.out.println(makeGood(str));
    }

    static String makeGood(String s) {
        String str = "";
        while (true) {
            int n = s.length();
            str = "";
            int i = 0;
            int flag = 0;
            while (i < n) {
                if (i < n - 1) {
                    if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
                        char y = s.charAt(i + 1);
                        if (y >= 'A' && y <= 'Z') {
                            if (y + 32 == s.charAt(i)) {
                                flag = 1;
                                i += 2;
                            } else {
                                str += s.charAt(i);
                                i++;
                            }
                        } else {
                            str += s.charAt(i);
                            i++;
                        }
                    } else if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
                        char y = s.charAt(i + 1);
                        if (y >= 'a' && y <= 'z') {
                            if (y - 32 == s.charAt(i)) {
                                i += 2;
                                flag = 1;
                            } else {
                                str += s.charAt(i);
                                i++;
                            }
                        } else {
                            str += s.charAt(i);
                            i++;
                        }
                    }
                } else {
                    str += s.charAt(i);
                    i++;
                }
            }
            s = str;
            if (flag == 0)
                break;
        }
        return str;
    }

}

C++

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

string makeGood(string s)
{
    string str = "";
    while (true)
    {
        int n = s.size();
        str = "";
        int i = 0;
        int flag = 0;
        while (i < n)
        {
            if (i < n - 1)
            {
                if (s[i] >= 'a' && s[i] <= 'z')
                {
                    char y = s[i + 1];
                    if (y >= 'A' && y <= 'Z')
                    {
                        if (y + 32 == s[i])
                        {
                            flag = 1;
                            i += 2;
                        }
                        else
                        {
                            str += s[i];
                            i++;
                        }
                    }
                    else
                    {
                        str += s[i];
                        i++;
                    }
                }
                else if (s[i] >= 'A' && s[i] <= 'Z')
                {
                    char y = s[i + 1];
                    if (y >= 'a' && y <= 'z')
                    {
                        if (y - 32 == s[i])
                        {
                            i += 2;
                            flag = 1;
                        }
                        else
                        {
                            str += s[i];
                            i++;
                        }
                    }
                    else
                    {
                        str += s[i];
                        i++;
                    }
                }
            }
            else
            {
                str += s[i];
                i++;
            }
        }
        s = str;
        if (flag == 0)
            break;
    }
    return str;
}

int main()
{
    string str = "aDdCD";
    cout << makeGood(str);
    return 0;
}


No comments:

Post a Comment