File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } A
3
+ * @param {string } B
4
+ * @return {number }
5
+ */
6
+ const kSimilarity = function ( A , B ) {
7
+ if ( A === B ) return 0
8
+ let arr = [ [ B , 0 ] ]
9
+ while ( arr . length > 0 ) {
10
+ let len = arr . length
11
+ for ( let i = 0 ; i < len ; i ++ ) {
12
+ let [ cur , step ] = arr . shift ( )
13
+ for ( let i = 0 ; i < cur . length ; i ++ ) {
14
+ if ( cur [ i ] === A [ i ] ) continue
15
+ for ( let j = i + 1 ; j < cur . length ; j ++ ) {
16
+ if ( cur [ j ] !== A [ i ] ) continue
17
+ let newStr =
18
+ cur . substring ( 0 , i ) +
19
+ cur [ j ] +
20
+ cur . substring ( i + 1 , j ) +
21
+ cur [ i ] +
22
+ cur . substring ( j + 1 )
23
+ if ( newStr === A ) return step + 1
24
+ if ( cur [ i ] === A [ j ] ) arr . unshift ( [ newStr , step + 1 ] )
25
+ else arr . push ( [ newStr , step + 1 ] )
26
+ }
27
+ break
28
+ }
29
+ }
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments