• 1405. Longest Happy String


    A string is called happy if it does not have any of the strings 'aaa''bbb' or 'ccc' as a substring.

    Given three integers ab and c, return any string s, which satisfies following conditions:

    • s is happy and longest possible.
    • s contains at most a occurrences of the letter 'a', at most b occurrences of the letter 'b' and at most c occurrences of the letter 'c'.
    • will only contain 'a''b' and 'c' letters.

    If there is no such string s return the empty string "".

    Example 1:

    Input: a = 1, b = 1, c = 7
    Output: "ccaccbcc"
    Explanation: "ccbccacc" would also be a correct answer.
    

    Example 2:

    Input: a = 2, b = 2, c = 1
    Output: "aabbc"
    

    Example 3:

    Input: a = 7, b = 1, c = 0
    Output: "aabaa"
    Explanation: It's the only correct answer in this case.
    

    Constraints:

    • 0 <= a, b, c <= 100
    • a + b + c > 0
    class Solution {
        String generate(int a, int b, int c, String aa, String bb, String cc) {
            if (a < b)
                return generate(b, a, c, bb, aa, cc);
            if (b < c)
                return generate(a, c, b, aa, cc, bb);
            if (b == 0)
                return aa.repeat(Math.min(2, a));
            int use_a = Math.min(2, a), use_b = a - use_a >= b ? 1 : 0; 
            return aa.repeat(use_a) + bb.repeat(use_b) +
                generate(a - use_a, b - use_b, c, aa, bb, cc);    
        }
        public String longestDiverseString(int a, int b, int c) {
            return generate(a, b, c, "a", "b", "c");
        }
    }

    屌。。https://leetcode.com/problems/longest-happy-string/discuss/564277/C%2B%2BJava-a-greater-b-greater-c

    class Solution {
        public String longestDiverseString(int a, int b, int c) {
            StringBuilder sb = new StringBuilder();
            int size = a + b + c;
            int A = 0, B = 0, C = 0;
            for(int i = 0; i < size; i++) {
                if ((a >= b && a >= c && A != 2) || (B == 2 && a > 0) || (C == 2 && a > 0))  {
                    sb.append("a");
                    a--;
                    A++;
                    B = 0;
                    C = 0;  
                } else if ((b >= a && b >= c && B != 2) || (A == 2 && b > 0) || (C == 2 && b > 0)) {
                    sb.append("b");
                    b--;
                    B++;
                    A = 0;
                    C = 0;
                } else if ((c >= a && c >= b && C != 2) || (B == 2 && c > 0) || (A == 2 && c > 0)) {
                    sb.append("c");
                    c--;
                    C++;
                    A = 0;
                    B = 0;  
                }
            }
            return sb.toString();
        }
    }

    屌。。。https://leetcode.com/problems/longest-happy-string/discuss/564273/Java-Happy-Greedy-String-without-PQ

  • 相关阅读:
    java学习的第三天
    java学习的第二天
    java学习的第一天
    兼容性测试2
    兼容性测试
    安全性测试
    界面测试
    功能性测试
    简历小技巧
    day13
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13091847.html
Copyright © 2020-2023  润新知