Socowi - 7 months ago 26

Perl Question

I have two strings

`$base`

`$succ`

`$succ`

`$base`

Some examples, written as isSucc(

`$base`

`$succ`

- isSucc("abc", "abcX") is
**true**, since we can insert X at the end of abc. - isSucc("abc", "XabYcZ") is
**true**, since we can insert**X**a**Y**b**Z**. - isSucc("abc", "abX") is
**false**, since c was deleted. - isSucc("abc", "cab") is
**false**, since c (the one at the end of abc) was deleted. - isSucc("abc", "cabc") is
**true**, since we can insert c at the beginning of abc. - isSucc("", "anything") is
**true**, since we just have to insert "anything".

We can assume, that

`$base`

`$succ`

`length($base) < length($length) < 1000`

I already know one way to implement

`isSucc`

- Is there a perl-module that defines something similar to ?
`isSucc`

- Does someone has an easier/faster/alternative implementation* of ?
`isSucc`

Compute the edit/Levenshtein distance with a custom cost model (

Answer

```
sub isSucc {
my ($base, $succ) = @_;
my $si = -1;
for my $c (split //, $base) { # for each character $c in $base
$si = index $succ, $c, $si+1; # find it after the previous one in $succ
return 0 if -1 == $si; # return false if we can't
}
return 1; # return true if we found all the characters in $base inside $succ
}
```

Source (Stackoverflow)