File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
cracking_the_coding_interview_problems Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * Cracking the coding interview edition 6
3
+ * Problem 1.5
4
+ * Three possible types of edit possible on a string
5
+ * Insert a char, delete a char or replace a char
6
+ * Write a function to check if two given string are one ( or zero ) edit(s) away
7
+ */
8
+
9
+
10
+ bool oneEditReplace ( const std::string & src, const std::string & dst )
11
+ {
12
+ int mismatch = 0 ;
13
+ for ( size_t i = 0 ; i < src.length (); ++i )
14
+ {
15
+ if ( src[i] != dst[i] )
16
+ {
17
+ ++mismatch;
18
+ }
19
+ }
20
+ return (mismatch <= 1 );
21
+ }
22
+
23
+ bool oneEditInsert ( const std::string & src, const std::string & dst )
24
+ {
25
+
26
+
27
+ }
28
+
29
+
30
+
31
+ bool oneEditAway ( const std::string & src, const std::string & dst )
32
+ {
33
+ int srcLen = src.length ();
34
+ int dstLen = dst.length ();
35
+ if ( srcLen == dstLen )
36
+ {
37
+ return oneEditReplace (src, dst);
38
+ }
39
+ else if ( srcLen + 1 == dstLen )
40
+ {
41
+ return oneEditInsert (src, dst);
42
+ }
43
+ else if ( srcLen - 1 == dstLen )
44
+ {
45
+ return oneEditRemove (src, dst);
46
+ }
47
+ return false ;
48
+ }
You can’t perform that action at this time.
0 commit comments