Sort by

recency

|

67 Discussions

|

  • + 0 comments

    wow very misleading question.

    spent alot of time, in vain, using the standard dynamic programming analysis, got no where because the choices of reduction for a string cant seem to be reduced to choices of reduction for its substrings, so the problem cant seem to be analyzed in terms of its subproblems - irreducible problem.

    turns out the problem just requires finding the parity invariant under reductions, since any non constant string can be reduced to length 1 or 2 which have different parity composition.

    O(n) no dynamic programming. very stupid question, why is it in dynamic programming when its just a trick question?

    int stringReduction(int n, const string& s) {
        vector<int> occurrence(3);
        for (char c : s) occurrence[c - 'a']++;
        if (occurrence[0] == n or occurrence[1] == n or occurrence[2] == n) return n;
        if (occurrence[0] % 2 == occurrence[1] % 2 and occurrence[1] % 2 == occurrence[2] % 2) return 2;
        else return 1;
    }
    
    int main()
    {
        int t;
        string s;
        cin >> t;
        for (int i=1; i <= t; i++) {
            cin >> s;
            cout << stringReduction(s.size(), s) << '\n';
        }
    }
    
  • + 0 comments

    Python:

    def stringReduction(s):
        c = [s.count(chr(i)) % 2 for i in range(97, 100)]
        return (1, 2)[sum(c) in (0, 3)] if len(set(s)) !=1 else len(s)
    
  • + 0 comments

    Here is my solution in java, javascript, python, C, C++, Csharp HackerRank String Reduction Problem Solution

  • + 0 comments

    Here is String Reduction problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-string-reduction-problem-solution.html

  • + 0 comments

    l = [s.count('a'),s.count('b'),s.count('c')) while(l.count(0)<2): l.sort(reverse=True) l[0]-=1 l[1] -= 1 l[2] += 1 return max(l)