@@ -4,65 +4,65 @@ Given 2 strings write a function that will check if it is
441 or 0 edits away*/
55
66const isOneAway = ( str1 , str2 ) => {
7+ const diffInLen = Math . abs ( str1 . length - str2 . length ) ;
8+ if ( diffInLen > 1 ) {
9+ return false ;
10+ }
11+
12+ let errorCount = 0 ;
713 if ( str1 . length === str2 . length ) {
8- let errorCount = 0 ;
914 for ( let i = 0 ; i < str1 . length ; i ++ ) {
10- if ( str1 [ i ] === str2 [ i ] ) {
11- continue ;
12- }
13- errorCount ++ ;
14- if ( errorCount > 1 ) {
15- return false ;
15+ if ( str1 [ i ] !== str2 [ i ] ) {
16+ errorCount ++ ;
17+ if ( errorCount > 1 ) {
18+ return false ;
19+ }
1620 }
1721 }
1822 } else {
19- let errorCount = 0 ;
20- const longestStr = findLongestStr ( str1 , str2 ) ;
21- let x = 0 ;
22- for ( let i = 0 ; i < longestStr . length ; i ++ ) {
23- let currentChar1 = str1 [ i ] ;
24- let currentChar2 = str2 [ x ]
25- let nextChar1 = str1 [ i + 1 ] ;
26- let nextChar2 = str2 [ x + 1 ] ;
27- if ( str1 [ i ] === str2 [ x ] ) {
28- x ++ ;
29- continue ;
30- }
31- errorCount ++ ;
32- if ( errorCount > 1 ) {
33- return false ;
23+ const longStr = str1 . length > str2 . length ? str1 : str2 ;
24+ const shortStr = str1 . length < str2 . length ? str1 : str2 ;
25+ for (
26+ let i = 0 , x = 0 ;
27+ i < longStr . length && x < shortStr . length ;
28+ i ++ , x ++
29+ ) {
30+ if ( longStr [ i ] !== shortStr [ x ] ) {
31+ errorCount ++ ;
32+ if ( errorCount > 1 ) {
33+ return false ;
34+ }
35+ i ++ ;
3436 }
35- if ( currentChar1 === nextChar2 ) {
36- x ++ ;
37- i -- ;
38- continue ;
39- } else if ( currentChar2 === nextChar1 ) {
40- continue ;
41- }
42- errorCount ++ ;
43- return false ;
44- }
45- // ***** Helper functions ********
46- function findLongestStr ( str1 , str2 ) {
47- return ( str1 . length > str2 . length ? str1 : str2 ) ;
4837 }
4938 }
5039 return true ;
51- }
40+ } ;
5241
5342// TESTS
54- console . log ( isOneAway ( 'pale' , 'ple' ) === true ) ;
55- console . log ( isOneAway ( 'pales' , 'pale' ) === true ) ;
56- console . log ( isOneAway ( 'pale' , 'bale' ) === true ) ;
57- console . log ( isOneAway ( 'pale' , 'bake' ) === false ) ;
58- console . log ( isOneAway ( 'paleo' , 'palseo' ) === true ) ;
43+ console . log ( isOneAway ( 'pale' , 'ple' ) === true ) ; // deletion
44+ console . log ( isOneAway ( 'pale' , 'opale' ) === true ) ; // insertion in beginning
45+ console . log ( isOneAway ( 'pale' , 'palse' ) === true ) ; // insertion in middle
46+ console . log ( isOneAway ( 'pale' , 'pales' ) === true ) ; // insertion at end
47+ console . log ( isOneAway ( 'pale' , 'bale' ) === true ) ; // replacement
48+ console . log ( isOneAway ( 'pale' , 'ae' ) === false ) ; // greater than 1 deletions
49+ console . log ( isOneAway ( 'pale' , 'ppalpe' ) === false ) ; // greater than 1 insertions
50+ console . log ( isOneAway ( 'pale' , 'bake' ) === false ) ; // greater than 1 replacements
51+ console . log ( isOneAway ( 'pale' , 'balpe' ) === false ) ; // 1 insertion, 1 replacement
52+ console . log ( isOneAway ( 'pale' , 'plo' ) === false ) ; // 1 deletion, 1 replacement
53+ console . log ( isOneAway ( 'pale' , 'ales' ) === false ) ; // 1 deletion, 1 insertion
54+ // swap str1 with str2
55+ console . log ( isOneAway ( 'ple' , 'pale' ) === true ) ; // deletion
56+ console . log ( isOneAway ( 'opale' , 'pale' ) === true ) ; // insertion in beginning
57+ console . log ( isOneAway ( 'palse' , 'pale' ) === true ) ; // insertion in middle
58+ console . log ( isOneAway ( 'pales' , 'pale' ) === true ) ; // insertion at end
59+ console . log ( isOneAway ( 'bale' , 'pale' ) === true ) ; // replacement
60+ console . log ( isOneAway ( 'ae' , 'pale' ) === false ) ; // greater than 1 deletions
61+ console . log ( isOneAway ( 'ppalpe' , 'pale' ) === false ) ; // greater than 1 insertions
62+ console . log ( isOneAway ( 'bake' , 'pale' ) === false ) ; // greater than 1 replacements
63+ console . log ( isOneAway ( 'balpe' , 'pale' ) === false ) ; // 1 insertion, 1 replacement
64+ console . log ( isOneAway ( 'plo' , 'pale' ) === false ) ; // 1 deletion, 1 replacement
65+ console . log ( isOneAway ( 'ales' , 'pale' ) === false ) ; // 1 deletion, 1 insertion
5966console . log ( isOneAway ( 'p' , 'b' ) === true ) ;
6067console . log ( isOneAway ( '' , '' ) === true ) ;
6168console . log ( isOneAway ( 'p' , 'p' ) === true ) ;
62- console . log ( isOneAway ( 'pale' , 'ppalpe' ) === false ) ;
63-
64- console . log ( isOneAway ( 'ple' , 'pale' ) === true ) ;
65- console . log ( isOneAway ( 'pale' , 'pales' ) === true ) ;
66- console . log ( isOneAway ( 'bale' , 'pale' ) === true ) ;
67- console . log ( isOneAway ( 'bake' , 'pale' ) === false ) ;
68- console . log ( isOneAway ( 'palseo' , 'paleo' ) === true ) ;
0 commit comments