318. Maximum Product of Word Lengths

本文介绍了一种寻找两个字符串中不重复字符的方法,并通过位运算来高效判断两个单词是否有共同字母,以此来找出最大长度乘积的两个单词。同时,还提到了另一种类似的问题,即在一个包含0和1的矩阵中寻找四个角都是1的矩形数量。

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.

Example 1:

Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
Return 16
The two words can be "abcw", "xtfn".

Example 2:

Given ["a", "ab", "abc", "d", "cd", "bcd", "abcd"]
Return 4
The two words can be "ab", "cd".

Example 3:

Given ["a", "aa", "aaa", "aaaa"]
Return 0

No such pair of words.

Solution:

The idea to compare whether two words has the same letter is to use bit representation for each word, or int. 

a is the last bit of the int and z is the 26th bit in the int.

CODE:

public class Solution {
    public int maxProduct(String[] words) {
        int[] bits = new int[words.length];
        for(int i = 0 ; i < words.length; i++){
            for(char c : words[i].toCharArray()){
                bits[i] |= 1 << (c - 'a');
            }
        }
        
        int ret = 0;
        for(int i = 0 ; i < bits.length - 1; i++){
            for(int j = 0; j < bits.length; j++){
                if((bits[i] & bits[j]) == 0){
                    ret = Math.max(ret,words[i].length() * words[j].length());
                }
            }
        }
        
        return ret;
    }
}

Another similar question is that there is a matrix contains 0s and 1s, find the number of rectangle where all the four corner is 1.

if we check to rows, 

111011,

011110

and do the & operation.

011010, there are 3 1s in the result, so the number would be pick 2 out of 3 is 6;

 





根据给定的参考内容,未直接涉及到解决 'Invalid categorical variables. Too few items. Please specify: Distinct columns of equal length. Minimum number of columns:1. Maximum number of columns:4.' 错误的信息。不过,从错误提示本身可以分析出解决思路。 该错误提示表明在处理分类变量时,存在分类变量项过少的问题,并且要求指定不同且等长的列,列数范围在 1 到 4 列之间。以下是可能的解决步骤和示例代码: ### 检查数据 首先要确保输入的数据满足列数和长度要求。可以通过查看数据的形状和内容来进行检查。 ```python import pandas as pd # 假设 data 是你的 DataFrame data = pd.DataFrame({ 'col1': [1, 2, 3], 'col2': [4, 5, 6] }) # 检查列数 num_columns = data.shape[1] if num_columns < 1 or num_columns > 4: print("列数不符合要求,需要 1 到 4 列。") # 检查列的长度是否相等 column_lengths = [len(data[col]) for col in data.columns] if len(set(column_lengths)) != 1: print("列的长度不相等。") ``` ### 筛选和调整数据 如果列数或列长度不符合要求,可以对数据进行筛选或调整。 ```python # 筛选出符合列数要求的列 if num_columns > 4: data = data.iloc[:, :4] # 确保列长度相等(如果不相等,可以根据具体情况进行处理,这里简单假设可以截断) min_length = min(column_lengths) data = data.head(min_length) ``` ### 确保列的独特性 要保证每列的数据是不同的。 ```python # 检查列是否重复 if len(set(data.columns)) != len(data.columns): data = data.loc[:, ~data.columns.duplicated()] ``` ### 示例完整代码 ```python import pandas as pd # 假设 data 是你的 DataFrame data = pd.DataFrame({ 'col1': [1, 2, 3], 'col2': [4, 5, 6], 'col3': [7, 8, 9], 'col4': [10, 11, 12], 'col5': [13, 14, 15] }) # 检查列数 num_columns = data.shape[1] if num_columns < 1 or num_columns > 4: print("列数不符合要求,需要 1 到 4 列。") if num_columns > 4: data = data.iloc[:, :4] # 检查列的长度是否相等 column_lengths = [len(data[col]) for col in data.columns] if len(set(column_lengths)) != 1: print("列的长度不相等。") min_length = min(column_lengths) data = data.head(min_length) # 检查列是否重复 if len(set(data.columns)) != len(data.columns): data = data.loc[:, ~data.columns.duplicated()] print("处理后的数据:") print(data) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值