Java:求字符串数组中两组字符串长度乘积最大值(且两字符串无相同字符)

package com.zbj.demo;

import org.junit.Test;

public class 字符串数组中长度乘积最大值_不含重复字符 {
	//String[] words = {"aaa","abc","acc","adc"};
	String[] words = {"abcd","efgh","axyz","opqrs"};
	
	@Test
	public void demo() {
		int maxLength = 0; //定义最大长度
		
		for (int i = 0; i < words.length-1; i++) { //1.使用双层循环比较所有字符串
			
			for (int j = i+1; j < words.length; j++) {
				char[] iArray = words[i].toCharArray(); //2.将字符串转换为字符数组,遍历时使用
				char[] jArray = words[j].toCharArray(); //同上
				
				boolean flag = true; //3.所有比较走完都不改变则说明没有相同的字符
				//注:这里jj应该是从0开始遍历
				for (int ii = 0; ii < iArray.length; ii++) { //4.比较两个字符串中的所有字符是否有相同字符
					for (int jj = 0; jj < jArray.length; jj++) {
						if(iArray[ii]==jArray[jj]) {
							flag = false;
							break;
						}
					}
				}
				
				if(flag) { //5.如果为真就说明是两个不相同的字符串
					int newLength = iArray.length*jArray.length; //6.新的长度值
					maxLength = maxLength>newLength?maxLength:newLength; //7.如果已赋值的maxLength大就不改变值,否则就替换
				}
				
				
			}
		}
		//8.输出最大值
		System.out.println(maxLength);
	}
}

你可能感兴趣的:(Java:求字符串数组中两组字符串长度乘积最大值(且两字符串无相同字符))