34 lines
654 B
C++
34 lines
654 B
C++
#include <bits/stdc++.h>
|
||
using namespace std;
|
||
|
||
int main() {
|
||
string a = "pple";
|
||
string b = "apple";
|
||
|
||
int cnt = 0;
|
||
if (a.size() > b.size())
|
||
swap(a, b);
|
||
|
||
if (a.size() - b.size() > 1)
|
||
return -1;
|
||
|
||
// 保证a是短串,b是长串
|
||
int p = 0;
|
||
for (int i = 0; i < a.size();) {
|
||
// 遍历短串中每一个char
|
||
// 此时,长串中的位置是p
|
||
|
||
if (a[i] != b[p]) {
|
||
// 对不上的时候,p指针移动一位
|
||
p++;
|
||
cnt++;
|
||
if (cnt > 1)
|
||
return -1;
|
||
} else {
|
||
// 对上的时时候,p指针与i指针同步移动
|
||
p++;
|
||
i++;
|
||
}
|
||
}
|
||
return 0;
|
||
} |