File tree Expand file tree Collapse file tree 1 file changed +19
-9
lines changed Expand file tree Collapse file tree 1 file changed +19
-9
lines changed Original file line number Diff line number Diff line change 11function isOneEditAway ( str1 , str2 ) {
2- let letters = new Map ( ) ;
3- let sizeDelta = Math . abs ( str1 . length - str2 . length ) ;
2+ let edit = 0 ;
3+ let i1 , i2 ;
44
5- if ( sizeDelta > 1 ) {
6- return false ;
7- }
5+ for ( i1 = 0 , i2 = 0 ; i1 < str1 . length && i2 < str2 . length && edit <= 1 ; i1 ++ , i2 ++ ) {
6+ if ( str1 [ i1 ] === str2 [ i2 ] ) {
7+ continue ;
8+ } else {
9+ edit ++ ;
810
9- for ( const char of str1 . concat ( str2 ) ) {
10- letters . set ( char , ( letters . get ( char ) || 0 ) + 1 ) ;
11+ if ( str1 . length > str2 . length ) {
12+ i2 -- ;
13+ } else if ( str1 . length < str2 . length ) {
14+ i1 -- ;
15+ } else {
16+ continue ;
17+ }
18+ }
1119 }
1220
13- const oddLetters = Array . from ( letters . values ( ) ) . filter ( ( v ) => v % 2 ) ;
14- return oddLetters . length < 3 ;
21+ edit += str1 . length - 1 - i1 ;
22+ edit += str2 . length - 1 - i2 ;
23+
24+ return edit <= 1 ;
1525}
1626
1727console . log ( isOneEditAway ( 'pale' , 'pale' ) ) ; // true
You can’t perform that action at this time.
0 commit comments