File tree 2 files changed +116
-1
lines changed
2 files changed +116
-1
lines changed Original file line number Diff line number Diff line change
1
+ /* *
2
+ * @author : ashwek
3
+ * @date : 29/12/2018
4
+ */
5
+
6
+ #include <stdio.h>
7
+ #include <string.h>
8
+ #include <stdlib.h>
9
+
10
+ int oneEditAway (char Str1 [], char Str2 []){
11
+
12
+ int l1 = strlen (Str1 ), l2 = strlen (Str2 );
13
+ int i = 0 , j = 0 , diff = 0 ;
14
+
15
+ if ( abs (l1 - l2 ) > 1 )
16
+ return 0 ;
17
+
18
+ while ( diff < 2 && (i < l1 ) && (j < l2 ) ){
19
+
20
+ if ( Str1 [i ] == Str2 [j ] ){
21
+ i ++ ;
22
+ j ++ ;
23
+ }
24
+ else {
25
+ diff ++ ;
26
+ if ( l1 == l2 ){
27
+ i ++ ;
28
+ j ++ ;
29
+ }
30
+ else if ( l1 < l2 )
31
+ j ++ ;
32
+ else
33
+ i ++ ;
34
+ }
35
+ }
36
+
37
+ return diff < 2 ;
38
+
39
+ }
40
+
41
+ void main (){
42
+
43
+ char Str1 [20 ];
44
+ char Str2 [20 ];
45
+
46
+ printf ("Enter string 1 = " );
47
+ scanf ("%s" , Str1 );
48
+ printf ("Enter string 2 = " );
49
+ scanf ("%s" , Str2 );
50
+
51
+ printf ("%s & %s are " , Str1 , Str2 );
52
+ if ( oneEditAway (Str1 , Str2 ) == 0 )
53
+ printf ("NOT " );
54
+ printf ("one edit away\n" );
55
+ }
Original file line number Diff line number Diff line change @@ -455,4 +455,64 @@ int main()
455
455
}
456
456
return 0;
457
457
}
458
- ```
458
+ ```
459
+
460
+ ### [ Solution 2] ( ./C/oneEditAway.c )
461
+
462
+ ``` c
463
+ /* *
464
+ * @author : ashwek
465
+ * @date : 29/12/2018
466
+ */
467
+
468
+ #include < stdio.h>
469
+ #include < string.h>
470
+ #include < stdlib.h>
471
+
472
+ int oneEditAway (char Str1[ ] , char Str2[ ] ){
473
+
474
+ int l1 = strlen(Str1), l2 = strlen(Str2);
475
+ int i=0, j=0, diff=0;
476
+
477
+ if( abs(l1 - l2) > 1 )
478
+ return 0;
479
+
480
+ while( diff<2 && (i < l1) && (j < l2) ){
481
+
482
+ if( Str1[i] == Str2[j] ){
483
+ i++;
484
+ j++;
485
+ }
486
+ else{
487
+ diff++;
488
+ if( l1 == l2 ){
489
+ i++;
490
+ j++;
491
+ }
492
+ else if( l1 < l2 )
493
+ j++;
494
+ else
495
+ i++;
496
+ }
497
+ }
498
+
499
+ return diff < 2;
500
+
501
+ }
502
+
503
+ void main(){
504
+
505
+ char Str1[20];
506
+ char Str2[20];
507
+
508
+ printf("Enter string 1 = ");
509
+ scanf("%s", Str1);
510
+ printf("Enter string 2 = ");
511
+ scanf("%s", Str2);
512
+
513
+ printf("%s & %s are ", Str1, Str2);
514
+ if( oneEditAway(Str1, Str2) == 0 )
515
+ printf("NOT ");
516
+ printf("one edit away\n");
517
+ }
518
+ ```
You can’t perform that action at this time.
0 commit comments