找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

楼主: roommate
收起左侧

[Goldman Sachs] Goldman Sachs新鲜电面

[复制链接]

1173

主题

182

精华

3657

积分

神级会员

Rank: 7Rank: 7Rank: 7

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

Use Multimap. Use the sorted value of a string as Key. # p4 U, w! J9 t) o2 ]( Q# n
# G# ]6 a- I* x; W* F3 T6 c; i) K% \
So for tac. The key will be act. " Z% j/ u. l7 L9 Y6 l6 C5 d: ]. p
Also for atc. The key will be act.

1153

主题

172

精华

3562

积分

神级会员

Rank: 7Rank: 7Rank: 7

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

package com.careercup;7 }3 v; w- q) {& d
; A( P5 k4 V: {! W8 i4 N7 p6 a- C
public class FuzzyStringSearch
4 {4 X! M* ]) e' N, X. W4 `6 j{
1 h/ o+ ~0 M8 \! O! u    + u4 }5 u. C2 k  c
    public static void main(String[] args)
1 E5 ^7 h2 y  J9 k    {( a/ ~; v  c# ^# {/ H# J
       String input[] = new String[]{"cat","lion","act","dog"};
9 E$ A0 g8 d" T       String toSearch = "tac";% H% m; h( U+ N0 `4 F& ]
       searchWord(toSearch,input);+ x! ~6 Y. Q/ \2 t2 n
    }
7 f6 S2 q6 G6 [/ s/ y' e
7 B) e7 w3 J2 Q! H    private static void searchWord(String toSearch, String[] input)
& B, z. q7 m6 I3 I) w5 B  r; ~    {
7 t( K- O3 ~; G( e        for (int i = 0; i < input.length; i++)& I. O" r2 v. r! [" j7 Y& s
        {
; x1 M/ w0 A+ Z0 J+ ]            if (input[i].length() == toSearch.length())% L+ I; M, u1 j# u& L- O- w6 k+ u( X
            {0 [7 \+ h& D' l( t8 m7 S
                if(fuzzySearch(input[i],toSearch))
, e  i6 i0 S- k6 U4 ^$ I) n7 e! {                {) X5 l- o0 S& q
                    System.out.println(input[i]);
' A, q0 E# g8 T                }
7 u6 Y( C- [7 k5 w) g            }7 _3 V# ^2 Q% f- d0 @, b7 f
        }! C' r* [. @# b3 E9 r
    }
6 F" W+ I- e; U! d7 L. I+ |
2 x2 E7 p- V0 Y  l0 y) B6 v" S    private static boolean fuzzySearch(String string, String toSearch)2 o: ^5 s, r+ a8 F
    {
( Y6 ~8 e# i+ h% ^5 f: M, m5 f+ n        boolean result = false;9 A% |9 |  A/ @$ Y  R
        char text[] = toSearch.toCharArray();4 m+ Q# [& B! `  ~4 Q
        
+ ]1 V$ H7 V, w0 `        for (int i = 0; i < text.length; i++)
7 t8 B2 Y( h: |; B& S, t+ p: n        {6 B- w- c" }2 J
            for (int j = 0; j < string.length(); j++)1 U% c1 J: T. g8 ~
            {' I% V2 ]: E% w4 N, X
                if(text[i] == string.charAt(j))
. P" Q0 [( `% Q# a3 J                    result = true;
' }- G* x7 f1 V+ p+ m+ f                
' l7 m$ Q$ \+ I6 T            }- O& K6 r- _9 _# C$ Y8 Y
        }
: {/ }+ o! y% ]8 v9 T$ s        return result;( H, s- ^5 N! L6 G9 I9 C. x; L, M/ [: `
    }: s+ h" {! @9 m7 W
    ( g, R! k& I/ r% e
}

1139

主题

189

精华

3647

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3647
发表于 2-8-2017 11:03 PM | 显示全部楼层
! Z- o8 r6 R& e( X4 ~" S
5 i9 S8 f) a4 \. k5 C: A7 m+ L : x9 @2 B& P; a. v
0
3 U1 h" W$ i; P
6 p8 @- B/ P: a* k f 1 b: r3 R p' W" |6 V9 l6 R4 y
of 0 votes
" L1 {* n0 f7 e* r
0 ^, f3 x3 G* D; v( x/ E* H
/ X6 ^* C/ P4 V8 k 5 Y& A8 D, }1 M2 x

In the body of IF condition result=true does not always implies that the strings are anagrams.

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

本版积分规则

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