【华为OD机试真题 JAVA】 计算最大乘积【2022 Q4 | 100分】

前言

《华为OD笔试真题 JAVA》 专栏含华为OD机试真题JAVA实现、华为面试题、牛客网华为专栏真题。

如果您正在准备华为的面试,或者华为od的机会,希望可以帮到您! PS:文中答案仅供参考,不可照抄

题目描述

计算最大乘积

给定一个元素类型为小写字符串的数组,请计算两个没有相同字符的元素长度乘积的最大值,

如果没有符合条件的两个元素,返回0。

输入描述

输入为一个半角逗号分隔的小写字符串的数组,2 <= 数组长度<=100,0 < 字符串长度<= 50。

输出描述

两个没有相同字符的元素 长度乘积的最大值

示例1 输入输出示例仅供调试,后台判题数据一般不包含示例

输入

iwdvpbn,hk,iuop,iikd,kadgpf

输出

14

说明

数组中有5个元素。

iwdvpbn与hk无相同的字符,满足条件,iwdvpbn的长度为7,hk的长度为2,乘积为14(7*2)。

iwdvpbn与iuop、iikd、kadgpf均有相同的字符,不满足条件。

iuop与iikd、kadgpf均有相同的字符,不满足条件。

iikd与kadgpf有相同的字符,不满足条件。

因此,输出为14。

JAVA代码实现1:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] split = sc.nextLine().split(",");
        int max = 0;
        for (int i = 0; i < split.length; i++) {
            for (int j = i + 1; j < split.length; j++) {
                if (!checkHaveSame(split[i], split[j])) {
                    max = Math.max(max, split[i].length() * split[j].length());
                }
            }
        }
        System.out.println(max);
    }

    private static boolean checkHaveSame(String str1, String str2) {
        char[] chars1 = str1.toCharArray();
        char[] chars2 = str2.toCharArray();
        Arrays.sort(chars1);
        Arrays.sort(chars2);
        int idx1 = 0;
        int idx2 = 0;
        while (idx1 < str1.length() && idx2 < str2.length()) {
            if (chars1[idx1] == chars2[idx2]) {
                return true;
            } else if (chars1[idx1] > chars2[idx2]) {
                idx2++;
            } else if (chars1[idx1] < chars2[idx2]) {
                idx1++;
            }
        }
        return false;
    }
}


JAVA代码实现2:

import java.util.Scanner;

public class MaxProduct {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int maxValue = -1;
        String a = in.nextLine();
        if (a == null) {
            System.out.println(0);
        }
        assert a != null;
        String[] array = a.split(",");
        if (array.length == 1) {
            System.out.println(0);
        }
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (equals(array[i], array[j])) {
                    int x = array[i].length();
                    int y = array[j].length();
                    maxValue = Math.max(maxValue, x * y);
                }
            }
        }
        System.out.println(maxValue == -1 ? 0 : maxValue);
    }

    public static boolean equals(String a, String b) {
        for (int i = 0; i < a.length(); i++) {
            for (int j = 0; j < b.length(); j++) {
                if (a.charAt(i) == b.charAt(j)) {
                    return false;
                }
            }
        }
        return true;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值