找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

楼主: roommate
收起左侧

[Goldman Sachs] Goldman Sachs新鲜电面

[复制链接]

1161

主题

184

精华

3664

积分

神级会员

Rank: 7Rank: 7Rank: 7

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

import java.util.Arrays;

public class FindNumOfWordsFromList {

static String[] array = {"act","tca","asd","cat"};

public static void main(String[] args){

searchwords("act");
}

private static void searchwords(String string) {

for(String word:array){
if(!word.equals(string) && word.length()==string.length()){
char[] stringCh =string.toCharArray();
char[] wordCh =word.toCharArray();
Arrays.sort(stringCh);
Arrays.sort(wordCh);
if(Arrays.equals(stringCh, wordCh)){
System.out.println(word);
}
}

}
}
}

1154

主题

153

精华

3407

积分

神级会员

Rank: 7Rank: 7Rank: 7

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

import java.util.Arrays;" k! i- G" n/ h
/ G5 I& S) S. j0 F
public class FindNumOfWordsFromList {6 W0 t" h5 Y5 m7 e- F8 X
4 Z: o% k4 f' O7 d
	static String[] array = {"act","tca","asd","cat"};
& T9 ~+ T* O/ j# W# R. Z	$ V$ I% e) g: O+ q
	public static void main(String[] args){6 ^1 g2 D' y+ {- E
		
, m$ h9 ?2 `- ^% g! n		searchwords("act");
1 Z( l& Q, o; R! R1 o, g. w	}* l, e* k8 Q3 G8 v* S/ F
' S% c1 F& q. d: N; r* c
	private static void searchwords(String string) {
. e% w1 m; |$ G
! y6 i  [! E( v+ V		for(String word:array){, {' R+ ^2 n! m$ L5 l$ @+ ?
			if(!word.equals(string) && word.length()==string.length()){6 B* j* |' q0 y% `4 e: g1 }+ y
				char[] stringCh =string.toCharArray();- z! t" c4 C; {7 l7 [
				char[] wordCh =word.toCharArray();
" m( G1 O) Y: a  z4 g7 j. |				Arrays.sort(stringCh);) @7 M3 T) `' X% N
				Arrays.sort(wordCh);
& V! O) z/ \$ o) u) Y& ~				if(Arrays.equals(stringCh, wordCh)){
1 ~' p1 i$ d# j" ]7 w% l# \7 f( _					System.out.println(word);
+ M5 G3 V) X- T9 k( O2 T				}3 g* K9 x  G% d2 C
			}
" Z; d# {. g2 B- O3 r" o			
2 H2 T' T3 u3 M7 D' h		}
; p+ W; c+ o; E' j: G	}
+ c) W) ?1 E2 v7 n: q! s, @}

1183

主题

200

精华

3760

积分

神级会员

Rank: 7Rank: 7Rank: 7

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

public static void main(String[] args) {
String[] arr={"abc","cab","fdg","sbc","bac","bsc"};
String inputKey="cba";
System.out.println(getAllPaatern(arr,inputKey));
}

static List<String> getAllPaatern(String[] inputArray, String searchKey){
List<String> wordList= new ArrayList<String>();
for(int i=0;i <inputArray.length;i++){
if(getSortedString(inputArray[i].toLowerCase()).equals(getSortedString(searchKey.toLowerCase()))){
wordList.add(inputArray[i]);
}
}
return wordList;
}

static String getSortedString(String str){
char [] c = str.toCharArray();
Arrays.sort(c);
return new String(c);
}

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

本版积分规则

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