华为机试:查找兄弟单词

输入描述:

先输入字典中单词的个数,再输入n个单词作为字典单词。输入一个单词,查找其在字典中兄弟单词的个数再输入数字n

输出描述:

根据输入,输出查找到的兄弟单词的个数

实现:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class BrotherWord {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()){
			int n=sc.nextInt();
			String[] strs=new String[n];
			for(int i=0; i<n; i++){
				strs[i]=sc.next();
			}
			String str=sc.next();			
			int index=sc.nextInt();
			ArrayList<String> brother=new ArrayList<>();
			int count=0;
			for(int i=0; i<n; i++){
				if(isBrotherWord(str, strs[i])){
					brother.add(strs[i]);
					count++;
				}
			}
			Collections.sort(brother);
			System.out.println(count);
			if(index<=brother.size()){
				System.out.println(brother.get(index-1));
			}
		}

	}
	
	public static boolean isBrotherWord(String s1, String s2){
		if(s1.equals(s2)) return false;
		if(s1.length()!=s2.length()) return false;
		char[] c1=s1.toCharArray();
		char[] c2=s2.toCharArray();
		Arrays.sort(c1);
		Arrays.sort(c2);
		s1=String.copyValueOf(c1);
		s2=String.copyValueOf(c2);
		if(s1.equals(s2)) return true;
		else return false;
	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值