论坛里看到的华为面试题

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @项目名称:TestProject
 * @类名称:NewHua
 * @类描述:
 * @创建人:Ansj
 * @创建时间:2011-8-1 下午01:24:38
 * @修改备注:
 * @version 有个list 里面存储的是string 例如:
 * 			"abcd" "acbd" "sdf" "sfd" "as"
 *          "f",其中"abcd"和"acbd" 是一类的,
 *          他让把一类的string找出来,写程序实现
 */
public class NewHua {
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>() ;
		list.add("abcd") ;
		list.add("acbd") ;
		list.add("sdf") ;
		list.add("sfd") ;
		list.add("as") ;
		list.add("f") ;
		
		Map<String,String> map = new HashMap<String,String>() ;
		char[] chars = null ;
		String str = null ;
		String temp = null ;
		for (int i = 0; i < list.size(); i++) {
			str = list.get(i) ;
			chars = str.toCharArray() ;
			Arrays.sort(chars) ;
			temp = new String(chars) ;
			if(map.containsKey(temp)){
				map.put(temp, map.get(temp)+","+str) ;
			}else{
				map.put(temp, str) ;
			}
		}
		
		System.out.println(map);
	}
}

你可能感兴趣的:(面试题)