找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

楼主: roommate
收起左侧

[Goldman Sachs] Goldman Sachs新鲜电面

[复制链接]

1195

主题

170

精华

3596

积分

神级会员

Rank: 7Rank: 7Rank: 7

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

def anagrams(word, some_list):
) ?& R- ^* U- c: O# |+ k, K9 {    letters = list(word)( c& c' ]3 n! N* E1 |
    matches = []
3 S4 t) J. o/ l    
8 ~, X4 W% [$ Y2 V" g    for value in some_list:2 r5 P- d5 z% B3 W& o' G3 {: H9 n
        if sorted(list(value)) == sorted(letters):
. |  u& V3 {% G, b            matches.append(value)) v8 Z1 R- u) U9 T9 u
    return matches
: c# _3 f& J; g% c3 L3 b    2 Y6 l2 u$ N. |3 f% j

6 D' V. j( B* h4 k, \) A! u( Sbank = ['cat', 'tac', 'act', 'trick', 'bad']! T3 O+ k! R0 p
( a2 E% M2 L# }- f
print(anagrams('tca', bank)) #returns ['cat','tac','act']

1194

主题

191

精华

3785

积分

神级会员

Rank: 7Rank: 7Rank: 7

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

import java.util.*;
; n2 Q' I2 R0 ]1 X  M+ dimport java.util.regex.Pattern;
7 U) X& F0 v* Y7 f6 G) p" ~public class AllMatchedletters {
: B( p% e2 `1 e3 M; j! e2 u' S	4 o, {& ~, x4 _) z1 Q
	ArrayList<String> al=new ArrayList<String>();
# ?8 k/ {6 |6 C9 Z' |' E4 _1 B/ b	ArrayList <String>matches=new ArrayList<String>();
& ~0 U2 f; W4 Q* h. b* r9 V) J$ i	public void insertChar()# W( X, `! d6 a/ l0 Z+ s4 p
	{/ x0 d6 v  r8 ~% G! W: E8 U
		al.add("cat");$ f9 L4 y: \; T; S+ i% o6 g1 i
		al.add("tac");
* [& a/ F7 S7 S$ [. v' C		al.add("tas");
8 p/ u! Q7 i7 ?' ?% u9 `. {3 B		al.add("act");' ~" J/ c* q2 m, u' A
		al.add("bct");
6 t6 m; i6 R  W/ G. ]& m		al.add("bat");
% h: r2 s5 Q/ M% [& F" m: W	}4 z- F. Y  \8 C  i; s+ A, c
	public void matchAll(String str)
) l3 Y4 n! J, H& w/ J( u4 T: y	{7 O  q, x2 I, g+ b+ c
		Pattern p=Pattern.compile(str);3 O3 u& i2 t8 |+ Q; z# x. b0 f
		for(String s:al)
5 E4 Q+ o4 v8 K5 m: Z0 x& u* U		{% p) D6 Q$ K- g' ~- F
			if(p.matcher(s).matches())( Y: }) _) v, h8 F0 g, e
			{' @8 K9 _7 G1 A$ ?2 d0 |- _/ K
				matches.add(s);* ~3 X0 N  w6 A
			}; k4 M2 L% M% g$ X. |, H# @  i( z
					}
# f, v' |8 [7 n' K5 j% h		
# z) e- H9 T1 w		System.out.println(matches);! Y/ i! w5 i6 f/ C+ [
	}
: D* `3 {7 J3 J; w% Z# R# ~) N	public static void main(String[] args) {
3 s) R# Y$ i5 W* Z/ G, V9 T		// TODO Auto-generated method stub
% T6 B" k# ]% C  z		AllMatchedletters match=new AllMatchedletters();
$ e  F" ]/ V# F! o  \		match.insertChar();
+ }/ b7 ^6 x1 Y5 r- q5 O		String str="[cat]+";
. f0 j2 ]3 ~0 v1 L2 n9 M' D		match.matchAll(str);/ T: ?, [) l. I6 n( g6 r: _
			}
! w- {; f% Y4 C3 U/ }+ ^/ @% c4 A+ A) j- K  c
}

1141

主题

171

精华

3486

积分

神级会员

Rank: 7Rank: 7Rank: 7

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

This problem can be solved by sorting the words and then compairing it with given word. This is basically a problem of finding anagrams from list of words.

<pre>
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AnagramFinder {

public static void main(String[] args) {
List<String> listOfWords = new ArrayList<String>();
listOfWords.add("utid");
listOfWords.add("ttid");
listOfWords.add("diut");
listOfWords.add("utdi");

findAnagramInList(listOfWords,"tdiu");
}

private static void findAnagramInList(List<String> listOfWords,
String string) {
char[] stringArray= string.toCharArray();
Arrays.sort(stringArray);
for(String s :listOfWords){
char [] arr = s.toCharArray();
Arrays.sort(arr);

if(String.valueOf(arr).equals(String.valueOf(stringArray))){
System.out.println("Angram found :" +s);
}
}

}

}

</pre>

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

本版积分规则

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