Skip to content

Commit

Permalink
feat: add solutions to lcci problem: No.01.05
Browse files Browse the repository at this point in the history
No.01.05.One Away
  • Loading branch information
YangFong committed May 13, 2022
1 parent af39384 commit 9581b48
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 123 deletions.
84 changes: 43 additions & 41 deletions lcci/01.05.One Away/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,58 +195,60 @@ func min(x, y, z int) int {
}
```

### **TypeScript**

```ts
function oneEditAway(first: string, second: string): boolean {
const n = first.length;
const m = second.length;

let count = 0;
let i = 0;
let j = 0;
while (i < n || j < m) {
if (first[i] !== second[j]) {
count++;

if (i < n && first[i + 1] === second[j]) {
i++;
} else if (j < m && first[i] === second[j + 1]) {
j++;
}
}
i++;
j++;
}
return count <= 1;
}
```

### **Rust**

```rust
impl Solution {
pub fn one_edit_away(first: String, second: String) -> bool {
let f_len = first.len();
let s_len = second.len();
let first: Vec<char> = first.chars().collect();
let second: Vec<char> = second.chars().collect();
match f_len.max(s_len) - f_len.min(s_len) {
0 => {
let mut diff = false;
for i in 0..f_len {
if first[i] != second[i] {
if diff {
return false;
} else {
diff = true;
}
}
let (f_len, s_len) = (first.len(), second.len());
let (first, second) = (first.as_bytes(), second.as_bytes());
let (mut i, mut j) = (0, 0);
let mut count = 0;
while i < f_len && j < s_len {
if first[i] != second[j] {
if count > 0 {
return false;
}
true
}
1 => {
let mut diff = false;
let mut i = 0;
let mut j = 0;
while i < f_len && j < s_len {
if first[i] != second[j] {
if diff {
return false;
} else {
diff = true;
if i + 1 != f_len && first[i + 1] == second[j] {
i += 1;
} else if j + 1 != s_len && first[i] == second[j + 1] {
j += 1;
} else {
return false;
}
}
}

count += 1;
if i + 1 < f_len && first[i + 1] == second[j] {
i += 1;
} else if j + 1 < s_len && first[i] == second[j + 1] {
j += 1;
}
if diff && (i != f_len || j != s_len) {
return false;
}
true
}
_ => false,
i += 1;
j += 1;
}
count += f_len - i + s_len - j;
count <= 1
}
}
```
Expand Down
84 changes: 43 additions & 41 deletions lcci/01.05.One Away/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,58 +179,60 @@ func min(x, y, z int) int {
}
```

### **TypeScript**

```ts
function oneEditAway(first: string, second: string): boolean {
const n = first.length;
const m = second.length;

let count = 0;
let i = 0;
let j = 0;
while (i < n || j < m) {
if (first[i] !== second[j]) {
count++;

if (i < n && first[i + 1] === second[j]) {
i++;
} else if (j < m && first[i] === second[j + 1]) {
j++;
}
}
i++;
j++;
}
return count <= 1;
}
```

### **Rust**

```rust
impl Solution {
pub fn one_edit_away(first: String, second: String) -> bool {
let f_len = first.len();
let s_len = second.len();
let first: Vec<char> = first.chars().collect();
let second: Vec<char> = second.chars().collect();
match f_len.max(s_len) - f_len.min(s_len) {
0 => {
let mut diff = false;
for i in 0..f_len {
if first[i] != second[i] {
if diff {
return false;
} else {
diff = true;
}
}
let (f_len, s_len) = (first.len(), second.len());
let (first, second) = (first.as_bytes(), second.as_bytes());
let (mut i, mut j) = (0, 0);
let mut count = 0;
while i < f_len && j < s_len {
if first[i] != second[j] {
if count > 0 {
return false;
}
true
}
1 => {
let mut diff = false;
let mut i = 0;
let mut j = 0;
while i < f_len && j < s_len {
if first[i] != second[j] {
if diff {
return false;
} else {
diff = true;
if i + 1 != f_len && first[i + 1] == second[j] {
i += 1;
} else if j + 1 != s_len && first[i] == second[j + 1] {
j += 1;
} else {
return false;
}
}
}

count += 1;
if i + 1 < f_len && first[i + 1] == second[j] {
i += 1;
} else if j + 1 < s_len && first[i] == second[j + 1] {
j += 1;
}
if diff && (i != f_len || j != s_len) {
return false;
}
true
}
_ => false,
i += 1;
j += 1;
}
count += f_len - i + s_len - j;
count <= 1
}
}
```
Expand Down
57 changes: 16 additions & 41 deletions lcci/01.05.One Away/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,26 @@
impl Solution {
pub fn one_edit_away(first: String, second: String) -> bool {
let f_len = first.len();
let s_len = second.len();
let first: Vec<char> = first.chars().collect();
let second: Vec<char> = second.chars().collect();
match f_len.max(s_len) - f_len.min(s_len) {
0 => {
let mut diff = false;
for i in 0..f_len {
if first[i] != second[i] {
if diff {
return false;
} else {
diff = true;
}
}
let (f_len, s_len) = (first.len(), second.len());
let (first, second) = (first.as_bytes(), second.as_bytes());
let (mut i, mut j) = (0, 0);
let mut count = 0;
while i < f_len && j < s_len {
if first[i] != second[j] {
if count > 0 {
return false;
}
true
}
1 => {
let mut diff = false;
let mut i = 0;
let mut j = 0;
while i < f_len && j < s_len {
if first[i] != second[j] {
if diff {
return false;
} else {
diff = true;
if i + 1 != f_len && first[i + 1] == second[j] {
i += 1;
} else if j + 1 != s_len && first[i] == second[j + 1] {
j += 1;
} else {
return false;
}
}
}

count += 1;
if i + 1 < f_len && first[i + 1] == second[j] {
i += 1;
} else if j + 1 < s_len && first[i] == second[j + 1] {
j += 1;
}
if diff && (i != f_len || j != s_len) {
return false;
}
true
}
_ => false,
i += 1;
j += 1;
}
count += f_len - i + s_len - j;
count <= 1
}
}
22 changes: 22 additions & 0 deletions lcci/01.05.One Away/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function oneEditAway(first: string, second: string): boolean {
const n = first.length;
const m = second.length;

let count = 0;
let i = 0;
let j = 0;
while (i < n || j < m) {
if (first[i] !== second[j]) {
count++;

if (i < n && first[i + 1] === second[j]) {
i++;
} else if (j < m && first[i] === second[j + 1]) {
j++;
}
}
i++;
j++;
}
return count <= 1;
}

0 comments on commit 9581b48

Please sign in to comment.