# Solving Perl Weekly Challenge 092 -- Isomorphic Strings and Insert Interval.

The two tasks from Perl Weekly Challenge 092 seems to be useful inn some scenario.

## TASK #1 › Isomorphic Strings

You are given two strings \$A and \$B.

Write a script to check if the given strings are Isomorphic. Print 1 if they are otherwise 0.

Example 1:

``````Input: \$A = "abc"; \$B = "xyz"
Output: 1
``````

Example 2:

``````Input: \$A = "abb"; \$B = "xyy"
Output: 1
``````

Example 3:

``````Input: \$A = "sum"; \$B = "add"
Output: 0
``````

## Solution #1 › Isomorphic Strings

From the article "How to check if two strings are isomorphic", we could grasp the definition of "Isomorphic Strings". For string `\$A`, `\$B`, if there exists an character-mapping convert `\$A` to `\$B` and `\$B` to `\$A`, we say that this twoo strings are isomorphic. This seems to be similar to the definition of isomorphism in math.

With such definition in mind, to check if two strings are isomorphic, we shall gradually build the two-way character map by iterating through both of them character by character. In the way if any conflict happens, that means `\$A` and `\$B` are not isomorphic, otherwise they are.

Such algorithm can be translated to the follow Raku program:

``````sub isomorphic (Str \$A, Str \$B) {
my %trans;
return False unless \$A.chars == \$B.chars;

# 
for 0..^\$A.chars -> \$i {
my \$a = \$A.substr(\$i, 1);
my \$b = \$B.substr(\$i, 1);

if %trans{"ab"}{\$a}:exists and %trans{"ab"}{\$a} ne \$b {
return False
}
%trans{"ab"}{\$a} = \$b;

if %trans{"ba"}{\$b}:exists and %trans{"ba"}{\$b} ne \$a {
return False
}
%trans{"ba"}{\$b} = \$a;
}
return True;
}``````

It does not look very different from many other programming lanuanges except for some punctuations.

The beginnnig of `for` loop at  can be rewritten with `.comb` function and `Z operator` like this:

``````    for \$A.comb Z \$B.comb -> (\$a, \$b) {
...
}``````

Slightly shorter.

With `for` loop in Raku, we could iterate over multiple elements at a time and that's as easy as just adding more variables before the block:

``````my @S = (1,1,2,3,5,8,13,21,34);
for @S -> \$a,\$b,\$c {
say "\$a, \$b, \$c";
}
## Output
# 1, 1, 2
# 3, 5, 8
# 13, 21, 34``````

`Z` operator gives us a list of list. To capture the elements inside the inner list, we need wrap `\$a` and `\$b` inside a pair of parenthesis. Without, `\$a` and `\$b` captures the inner list themselfs. Consider the following program which demonstrates such differences:

``````my @S = (1,1,2,3,5,8,13,21);
my @T = (1,2,3,5,8,13,21,34);

# 
for @S Z @T -> \$a,\$b {
say "\$a, \$b";
}

# 
for @S Z @T -> (\$a,\$b) {
say "\$a, \$b";
}

## Output of 
# 1 1, 1 2
# 2 3, 3 5
# 5 8, 8 13
# 13 21, 21 34

## Output of 
# 1, 1
# 1, 2
# 2, 3
# 3, 5
# 5, 8
# 8, 13
# 13, 21
# 21, 34``````

To take a peek of what `@S Z @T` gives us, use `.gist` or `.raku`:

``````say (@S Z @T).gist;
#=> ((1 1) (1 2) (2 3) (3 5) (5 8) (8 13) (13 21) (21 34))

say (@S Z @T).raku;
#=> ((1, 1), (1, 2), (2, 3), (3, 5), (5, 8), (8, 13), (13, 21), (21, 34)).Seq``````

## TASK #2 › Insert Interval

You are given a set of sorted non-overlapping intervals and a new interval.

Write a script to merge the new interval to the given set of intervals. Example 1:

``````Input \$S = (1,4), (8,10); \$N = (2,6)
Output: (1,6), (8,10)
``````

Example 2:

``````Input \$S = (1,2), (3,7), (8,10); \$N = (5,8)
Output: (1,2), (3,10)
``````

Example 3:

``````Input \$S = (1,5), (7,9); \$N = (10,11)
Output: (1,5), (7,9), (10,11)
``````

## Solution #2 › Insert Interval

Merging multiple intervals as one, that would be a navie way to do compression.

The input `\$S` is sorted and there are no overlaps. However, `\$N` might be overlapping with one ore more elements inside `\$S`. The following Raku code is not probably the most efficient solution. Since `List` is immutable and I need to mutate the content of it, I use `@S` (`Array`) and make it a copy of `\$S` (`List`).

``````sub insert-intervals (@S is copy, \$N) {
# 
my \$i = @S.first(-> \$s { \$s <= \$N <= \$s }, :k);
my \$j = @S.first(-> \$s { \$s <= \$N <= \$s }, :k);

# 
if \$i.defined {
@S[\$i] = (@S[\$i], max(@S[\$i], \$N));
}

# 
if \$j.defined {
@S[\$j] = (@S[\$j], max(@S[\$j], \$N));

# 
if \$i.defined and @S[\$i] <= @S[\$j] <= @S[\$i] {
@S[\$i] = (@S[\$i], @S[\$j]);

for \$i^..\$j -> \$x {
@S[\$x] = Nil;
}
}
}

# 
if none(\$i.defined, \$j.defined) {
@S.push(\$N);
@S = @S.sort({ \$^a <=> \$^b });
}

# 
return @S.grep({ .defined });
}``````

At , we check whether `\$N` and `\$N` are individually overlaps with some elements in `@S`. If they are, we let `@S[\$i]` be the first element of `@S` that contains `\$N` and `@S[\$j]` be the first element of `@S` that contains `\$N`. Such arrangement is used latter from  to . Since `@S` is sorted, we can also assert `\$i ≤ \$j` here.

At , we merge `\$S[\$i]` with `\$N` if `\$N` is inside `@S[\$i]`.

At , we merge `\$S[\$j]` with `\$N` if `\$N` is inside `@S[\$j]`.

At , if both `\$i` and `\$j` are defined, that means the elements of `@S` in the range of `\$i..\$j` all overlap and they are all merged into `@S[\$i]`. Other elements arke marked as deletable.

At , we handle the case of when neither `\$i` nor `\$j` exists. This means `\$N` is not overlapping with any elemens of `@S`.

At , we delete the elements of `@S` that are marked as deletable from .

With all those edge cases, this does not seems to be an efficient solution. Perhaps I should work onn it a little bit more.