找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

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

[Goldman Sachs] Goldman Sachs新鲜电面

[复制链接]

1070

主题

136

精华

3119

积分

神级会员

Rank: 7Rank: 7Rank: 7

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

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

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

x
本帖最后由 Sophia 于 2-10-2017 10:08 AM 编辑
: X/ _$ a; E" Q* Q9 |: a9 y
& L) Z0 k) F4 V' j9 v' ]% j8 CYou have String array like{'cat','good','tac','act''....} like some 1000 words.
; ?! E; w/ ~" _  s; [: D! T" a# dSo if i give input tac ,output should be cat and act..
$ X- x% m- y0 \0 S1 C3 t" _How can we implement with less complexity

1135

主题

175

精华

3507

积分

神级会员

Rank: 7Rank: 7Rank: 7

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

private static void anagram(String[] str1,String searchString) {
* P' R/ G, q2 ~1 F6 b4 o1 l		// TODO Auto-generated method stub; O( W, N9 u6 F* a
		for(String s:str1); J! q* A* k, q, h8 G: E
		{6 w8 ^/ {8 S' w% M; c
			if(sort(searchString).equals(sort(s)))/ k" q4 o7 I; y+ R( u& ^7 d
			{- S7 Y* ?% L- j) m
				System.out.println(s);
$ T$ S. l5 e- c8 p8 P			}
0 `# }/ O& W2 o8 x" N7 Y		}) l9 H$ |% \, B% B+ J5 x/ K
	}

回复 支持 反对

使用道具 举报

1135

主题

146

精华

3340

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3340
发表于 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.& A; m4 @7 R N2 e5 B
: Q9 f+ g1 B/ n5 e9 i/ j9 X8 \4 G
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.7 S4 ]9 B x, Q
J6 u% _& W# A# k2 q. I
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.; q9 O6 D& l! o' z1 o- M/ {2 ?
* }$ F8 C8 G& m( p% q
- O(n * n) to build list, O(1) to print String list 7 w) Z, s9 Q. Z# D* t( ^
- O(n) space

回复 支持 反对

使用道具 举报

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

本版积分规则

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