1
+ //
2
+ // Created by Alex Hagiopol on 1/13/18.
3
+ //
4
+
5
+ /*
6
+ Chapter 01 - Problem 05 - One Away - CTCI 6th Edition page 91
7
+
8
+ Problem Statement:
9
+ There are three types of edits that can be performed on strings:
10
+ insert a character, remove a character, or replace a character.
11
+ Given two strings, write a function to check if they are one edit (or zero edits) away.
12
+
13
+ Example:
14
+ pale, ple -> true
15
+ pales, pale -> true
16
+ pale, bale -> true
17
+ pale, bae -> false
18
+
19
+ Solution:
20
+ 1. If the strings differ in length by more than 1 character, return false.
21
+
22
+ 2. If two strings differ in length by exactly one character, then search for an insertion / removal.
23
+ Traverse each string until a difference is found. If a difference is found, advance the index of the
24
+ longer string by 1 and the index of the shorter string by 0. If another difference is found, return false.
25
+ If no other difference is found, return true.
26
+
27
+ 3. If the strings have the same length, search for a replacement: traverse each string counting differences.
28
+ If more than one difference is found, return false else return true.
29
+
30
+ Time complexity: O(N) where N is the length of the shorter string.
31
+ Space complexity: O(1)
32
+ */
33
+
34
+ #include " problem_01_05_oneAway.h"
35
+ #include < stdlib.h>
36
+ #include < assert.h>
37
+
38
+ bool searchInsertion (const std::string &s1, const std::string &s2) {
39
+ assert (abs (static_cast <int >(s1.size () - s2.size ())) == 1 );
40
+ int i1 = 0 , i2 = 0 ;
41
+ bool foundDifference = false ;
42
+ while (i1 < s1.size () && i2 < s2.size ()){
43
+ if (s1[i1] != s2[i2]){
44
+ if (foundDifference){ // if difference already found, return false
45
+ return false ;
46
+ }
47
+ foundDifference = true ;
48
+ if (s1.size () > s2.size ()){ // if first difference found, assume we've found the place where a char was inserted
49
+ i1 ++;
50
+ } else {
51
+ i2 ++;
52
+ }
53
+ } else { // if no difference, continue iterating as normal
54
+ i1 ++;
55
+ i2 ++;
56
+ }
57
+ }
58
+ return true ;
59
+ }
60
+
61
+ bool searchReplacement (const std::string &s1, const std::string &s2) {
62
+ assert (s1.size () == s2.size ());
63
+ int i = 0 ;
64
+ bool foundDifference = false ;
65
+ while (i < s1.size ()){
66
+ if (s1[i] != s2[i]){
67
+ if (foundDifference){ // if more that one difference found, return false
68
+ return false ;
69
+ }
70
+ foundDifference = true ;
71
+ }
72
+ i ++;
73
+ }
74
+ return true ;
75
+ }
76
+
77
+ bool chapter_01::oneAway (const std::string &s1, const std::string &s2) {
78
+ int len_difference = abs (static_cast <int >(s1.size () - s2.size ()));
79
+ if (len_difference > 1 ){ // different by more than 1 edit: return false
80
+ return false ;
81
+ }
82
+ if (len_difference == 1 ){ // different by exactly one edit: could be an insertion / removal
83
+ return searchInsertion (s1, s2);
84
+ }
85
+ return searchReplacement (s1, s2); // same length: could be a replacement
86
+ }
0 commit comments