找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 7442|回复: 28
收起左侧

[Goldman Sachs] Goldman Sachs新鲜电面

[复制链接]

1087

主题

138

精华

3187

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3187
发表于 2-8-2017 11:03 PM | 显示全部楼层 |阅读模式

亲!马上注册或者登录会查看更多内容!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 Sophia 于 2-10-2017 10:08 AM 编辑   h7 ]* |1 r3 [8 v" I

, ]8 _: ?( M% n5 I8 nYou have String array like{'cat','good','tac','act''....} like some 1000 words.
" O' c& V/ H' V1 J7 [So if i give input tac ,output should be cat and act..
1 Z. {! K0 m" Q  CHow can we implement with less complexity

1151

主题

175

精华

3551

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3551
发表于 2-8-2017 11:03 PM | 显示全部楼层

private static void anagram(String[] str1,String searchString) {+ T1 [/ |, y  S$ ]
		// TODO Auto-generated method stub
, l' a) _! ~3 i* M+ J; Q' T' }		for(String s:str1)
5 ]5 D( a( s/ M: e/ p		{, u" j- k- ], Y  Z% u) e
			if(sort(searchString).equals(sort(s)))# m2 W( ~, W6 @1 I6 R8 T" g
			{& z( R7 V, V3 E' q7 {8 D% a( `( Z. ]
				System.out.println(s);
0 w9 E4 O4 Y% b. K& Z  g8 l			}
4 U/ j  K' d1 B: e0 k1 H5 u		}
# E5 y  ?, ~: v  V9 j	}

回复 支持 反对

使用道具 举报

1144

主题

147

精华

3369

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3369
发表于 2-8-2017 11:03 PM | 显示全部楼层

1. Create a HashMap<Integer, List<String>> that will hold every String from your array of strings using as Key the sum of each char Integer value. 8 r6 j) O6 G2 W N% Y
5 Q( @/ ~8 x5 [% t
2. Iterate over your list and insert each string using as key a transformation function that will calculate the sum of every char on that string, thus strings with same chars will have exact same sum. Append the Strings with same sum into the List of Strings for that particular sum.0 C0 A0 J1 |/ F/ L5 O
, B# `. O: W) X$ T
3. for your String as input, just get it's sum value using the same transformation function, once you have the sum use it as key to print the resulting List<String> of the HashMap, that will print all Strings with same chars. {* k$ g4 E9 {$ Y" T
# l& H" h4 t7 e1 s3 l. O
- O(n * n) to build list, O(1) to print String list* t& w) O2 T$ ^. z+ ?# {
- O(n) space

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表