@@ -4,65 +4,65 @@ Given 2 strings write a function that will check if it is
4
4
1 or 0 edits away*/
5
5
6
6
const 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 ;
7
13
if ( str1 . length === str2 . length ) {
8
- let errorCount = 0 ;
9
14
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
+ }
16
20
}
17
21
}
18
22
} 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 ++ ;
34
36
}
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 ) ;
48
37
}
49
38
}
50
39
return true ;
51
- }
40
+ } ;
52
41
53
42
// 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
59
66
console . log ( isOneAway ( 'p' , 'b' ) === true ) ;
60
67
console . log ( isOneAway ( '' , '' ) === true ) ;
61
68
console . 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