LeetCode 第 295 场周赛

6078. 重排字符形成目标字符串

题目

给你两个下标从 0 开始的字符串 s 和 target 。你可以从 s 取出一些字符并将其重排,得到若干新的字符串。

从 s 中取出字符并重新排列,返回可以形成 target 的 最大 副本数。

示例 1:

输入:s = "ilovecodingonleetcode", target = "code"
输出:2
解释:
对于 "code" 的第 1 个副本,选取下标为 4 、5 、6 和 7 的字符。
对于 "code" 的第 2 个副本,选取下标为 17 、18 、19 和 20 的字符。
形成的字符串分别是 "ecod" 和 "code" ,都可以重排为 "code" 。
可以形成最多 2 个 "code" 的副本,所以返回 2 。

示例 2:

输入:s = "abcba", target = "abc"
输出:1
解释:
选取下标为 0 、1 和 2 的字符,可以形成 "abc" 的 1 个副本。
可以形成最多 1 个 "abc" 的副本,所以返回 1 。
注意,尽管下标 3 和 4 分别有额外的 'a' 和 'b' ,但不能重用下标 2 处的 'c' ,所以无法形成 "abc" 的第 2 个副本。

示例 3:

输入:s = "abbaccaddaeea", target = "aaaaa"
输出:1
解释:
选取下标为 0 、3 、6 、9 和 12 的字符,可以形成 "aaaaa" 的 1 个副本。
可以形成最多 1 个 "aaaaa" 的副本,所以返回 1 。

提示:

1 <= s.length <= 100
1 <= target.length <= 10
s 和 target 由小写英文字母组成

思路

用桶将target和原字符所有出现的字符都记个数,然后再统计相同字符原字符所在的桶能容纳几次target所在桶的次数即可。

AC代码

点击查看代码
class Solution {
    public int rearrangeCharacters(String s, String target) {
        int[] arr = new int[26];
        int[] t = new int[26];
        for(int i=0; i<s.length(); i++) {
            int num = (int)(s.charAt(i)-'a');
            arr[num] ++;
        }
        for(int i=0; i<target.length(); i++) {
            int num = (int)(target.charAt(i)-'a');
            t[num] ++;
        }
        int ans = 105;
        for(int i=0; i<26; i++) {
            if( t[i]!=0 ) {
                ans = Math.min(ans, arr[i]/t[i]);
            }
        }
        return ans;
    }
}

6079. 价格减免

题目

句子 是由若干个单词组成的字符串,单词之间用单个空格分隔,其中每个单词可以包含数字、小写字母、和美元符号 '$' 。如果单词的形式为美元符号后跟着一个非负实数,那么这个单词就表示一个价格。

例如 "$100"、"$23" 和 "\(6.75" 表示价格,而 "100"、"\)" 和 "2$3" 不是。
注意:本题输入中的价格均为整数。

给你一个字符串 sentence 和一个整数 discount 。对于每个表示价格的单词,都在价格的基础上减免 discount% ,并 更新 该单词到句子中。所有更新后的价格应该表示为一个 恰好保留小数点后两位 的数字。

返回表示修改后句子的字符串。

示例 1:

输入:sentence = "there are $1 $2 and 5$ candies in the shop", discount = 50
输出:"there are $0.50 $1.00 and 5$ candies in the shop"
解释:
表示价格的单词是 "$1" 和 "$2" 。

  • "$1" 减免 50% 为 "$0.50" ,所以 "$1" 替换为 "$0.50" 。
  • "$2" 减免 50% 为 "$1" ,所以 "$1" 替换为 "$1.00" 。

示例 2:

输入:sentence = "1 2 $3 4 $5 $6 7 8$ $9 $10$", discount = 100
输出:"1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$"
解释:
任何价格减免 100% 都会得到 0 。
表示价格的单词分别是 "$3"、"$5"、"$6" 和 "$9"。
每个单词都替换为 "$0.00"。

提示:

1 <= sentence.length <= 105
sentence 由小写英文字母、数字、' ' 和 '$' 组成
sentence 不含前导和尾随空格
sentence 的所有单词都用单个空格分隔
所有价格都是 正 整数且不含前导零
所有价格 最多 为 10 位数字
0 <= discount <= 100

思路

模拟题,按照题目的意思将满足条件的价格计算下即可。

AC代码

点击查看代码
class Solution {
    
    private String calc(double price, int dis) {
        double ans = price * (double)(1.00-(double)(dis/100.0));
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(ans);
    }
    
    private boolean isPrice(String str) {
        if( str==null || str.length()<2 || str.charAt(0)!='$') {
            return false;
        }
        boolean point = false;
        for(int i=1; i<str.length(); i++) {
            char ch = str.charAt(i);
            if( '0'<=ch && '9'>=ch ) {
                continue;
            }
            if( ch=='.' && i!=1 && i!=str.length()-1 && !point ) {
                point = true;
            }
            return false;
        }
        return true;
    }
    
    public String discountPrices(String s, int discount) {
        String[] strs = s.split(" ");
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<strs.length; i++) {
            String str = strs[i];
            if( i!=0 ) {
                sb.append(" ");
            }
            if( isPrice(str) ) {
                double price = Double.parseDouble(str.substring(1));
                sb.append("$").append(calc(price, discount));
            } else {
                sb.append(str);
            }
        }
        return sb.toString();
    }
}
posted @ 2022-05-29 12:04  Asimple  阅读(39)  评论(0编辑  收藏  举报