【问题标题】:Validate string coupons which could be combo of other valid coupons验证可以是其他有效优惠券组合的字符串优惠券
【发布时间】:2022-01-16 01:25:16
【问题描述】:

这是亚马逊在 Hacker Rank 上提出的代码问题。请帮忙解决。

代码问题 在亚马逊的年度销售中,员工的任务是为忠实客户生成有效的折扣券。但是,组合中有一些已使用/无效的优惠券,此任务的挑战是确定给定的折扣优惠券是否有效。

折扣券的有效期确定如下:

  1. 空的折扣券有效。
  2. 如果优惠券 A 有效,则优惠券 C 是通过在 A 的开头都添加一个字符 x 制成的 而且A的结尾也是有效的(即优惠券C=xAx有效)。
  3. 如果两张优惠券A和Bare有效,那么B和A的串联也是有效的 (即优惠券 AB 和 BA 均有效)。

给定n张优惠券,每张优惠券只包含小写英文字符, 其中第 i 个折扣券表示为 discounts[i],判断每张折扣券是否有效。 在 answer 数组中有效的优惠券用 1 表示,无效的优惠券用 0 表示。

示例 折扣 = ['tabba'; 'abca']

检查此优惠券代码是否可以在有效优惠券的规则内构造。 检查“阿巴”: • 根据第一条规则,空字符串有效。 • 根据第二条规则,可以在有效优惠券代码的开头和结尾添加相同的字符。 在空字符串的开头和结尾添加“b”以获得有效代码“bb”。 • 使用相同的规则,将“a”添加到“bb”优惠券字符串的开头和结尾。同样,字符串是有效的。

字符串有效,所以答案数组为1

检查“abca”: • 使用规则 2,可以在字符串的两端添加一个字母,而不会改变其有效性。 添加到“bc”开头和结尾的“a”不会改变其有效性。 • 剩余的字符串“Ix”无效。没有规则允许在字符串的末尾添加不同的字符。

由于字符串无效,将 0 附加到答案数组。没有要测试的字符串,所以返回 [1,0]

功能说明

在下面的编辑器中完成find ValidDiscountCoupons函数。

find ValidDiscountCoupons 有以下参数: string discounts[n]:要验证的折扣券

返回 int[n]:如果优惠券折扣[il有效,则每个元素i为1,否则为0

我的解决方案(仅部分正确):

    public static List<int> findValidDiscountCoupons(List<string> discounts)
    {
        var r = new List<int>(); // result
        foreach (var s in discounts)
        {
            if (s == "")
                r.Add(1);
            else if (s.Length == 1)
                r.Add(0);
            else
            {
                if (isAllCharCountEven(s) && areCharPairsValid(s))
                    r.Add(1);
                else
                    r.Add(0);
            }
        }

        return r;
    }

    public static bool areCharPairsValid(string s)
    {
        char[] a = s.ToCharArray();

        int y = a.Length;

        for (int x = 0; x < y; x++)
        {
            if (x + 1 < y && a[x] == a[x + 1])
            {
                // two valid characteres together
                x++;
            }
            else if (a[x] == a[y - 1])
            {
                // chars at the front and the end of array match
                y--;
            }
            else
            {
                return false;
            }
        }

        return true;
    }

    public static bool isAllCharCountEven(string s)
    {
        while (s.Length > 0)
        {
            int count = 0;
            for (int j = 0; j < s.Length; j++)
            {
                if (s[0] == s[j])
                {
                    count++;
                }
            }

            if (count % 2 != 0)
                return false;

            s = s.Replace(s[0].ToString(), string.Empty);
        }

        return true;
    }

https://github.com/sam-klok/DiscountCouponesValidation

【问题讨论】:

  • d.length == 1 应该是无效字符串。
  • 每个字符的数量必须为偶数,但还不够。例如,acac 无效。
  • 我同意 cmets by user3386109 。代码已得到增强以匹配这些通知。

标签: arrays string algorithm


【解决方案1】:
    private static bool _IsValid(string input)
    {
        //When input is empty
        if (String.IsNullOrEmpty(input))
        {
            return true;
        }

        //When input is only one character
        if (input.Length == 1)
        {
            return false;
        }

        char[] inputChar = input.ToCharArray();

        //When input is two same characters
        if (input.Length == 2)
        {
            if (inputChar[0] == inputChar[1])
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //When input is > two character
        if (input.Length > 2)
        {
            //Split into two inputs
            for (int i = input.Length - 1; i > 0; i--)
            {
                if (inputChar[0] == inputChar[i])
                {
                    //Get first part
                    bool first = _IsValid(input[1..i]);

                    //Get second part
                    bool second = true;
                    if (first && i + 1 != input.Length)
                    {
                        second = _IsValid(input[(i + 1)..input.Length]);
                    }

                    return first && second;
                }
            }
        }
        return false;
  }

【讨论】:

  • 不正确的解决方案
  • @Abhishek 为什么?告诉我失败的情况
【解决方案2】:
public static void main(String[] args) {
    List<String> req = new ArrayList<>();
    req.add("abba");
    req.add("abca");
    req.add("daabbd");
    req.add("edabbaaccade");
    System.out.println(findValidDiscountCoupons(req)); 
}

public static List<Integer> findValidDiscountCoupons(List<String> discounts){
    List<Integer> res = new ArrayList<>();

    for(String s: discounts){
        if(checkIfCouponValid(s)){
            res.add(1);
        } else {
            res.add(0);
        }
    }

    return res;
}

public static boolean checkIfCouponValid(String coupon){

    // check if string is empty -> return true
    if(coupon.length() == 0){
        return true;
    }

    // check if string length is odd -> return false
    else if(coupon.length() % 2 == 1) {
        return false;
    }

    // divide string into 2 and check both strings
    else {
        String x = coupon.substring(0,coupon.length()/2);
        String y = coupon.substring(coupon.length()/2, coupon.length());
        
        // check string 1
        boolean validX = checkIfCouponValid(x);

        // check string 2
        boolean validY = checkIfCouponValid(y);

        // if both true -> return true
        if(validX && validY){
            return true;
        } else {
            
            // if first and last character of string are equal then remove them
            int left = 0;
            int right = coupon.length()-1;
            boolean removedAtleaseOneChar = false;
            
            if(coupon.charAt(left) == coupon.charAt(right)){
                left++;
                right--;
                removedAtleaseOneChar = true;
            }
            
            // If character not removed then string is not valid
            if(!removedAtleaseOneChar){
                return false;
            } else {
                coupon = coupon.substring(left, right+1);
            }
            
            return checkIfCouponValid(coupon);
        }
    }

}

【讨论】:

  • 解决方法不正确
  • @Abhishek 请提及解决方案失败的案例。
猜你喜欢
  • 1970-01-01
  • 2023-01-17
  • 1970-01-01
  • 2019-07-29
  • 1970-01-01
  • 2017-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多