aboutsummaryrefslogtreecommitdiff
path: root/challenge-082/duncan-c-white/perl/ch-2.pl
blob: bda9d018aba3b3278d6b536929f9bd072a7e5445 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/perl
#
# Task 2: "Interleave String
# 
# You are given 3 strings; $A, $B and $C.
# 
# Write a script to check if $C is created by interleave $A and $B.
# 
# Print 1 if check is success otherwise 0.
#
# Example 1:
# 
# Input:
#     $A = "XY"
#     $B = "X"
#     $C = "XXY"
# 
# Output: 1
# 
# EXPLANATION
# 
# "X" (from $B) + "XY" (from $A) = $C
# 
# Example 2:
# 
# Input:
#     $A = "XXY"
#     $B = "XXZ"
#     $C = "XXXXZY"
# 
# Output: 1
# 
# EXPLANATION
# 
# "XX" (from $A) + "XXZ" (from $B) + "Y" (from $A) = $C
# 
# Example 3:
# 
# Input:
#     $A = "YX"
#     $B = "X"
#     $C = "XXY"
# 
# Output: 0
# 
# My notes: It's not quite clear what "interleaving" means here, but I'm
# going to assume that it means "take a PREFIX of any length from one string,
# followed by a PREFIX of any length from the other string, then continue
# with the parts of the strings not yet consumed".  Relatively easy.
# 

use strict;
use warnings;
use Function::Parameters;
use feature 'say';
use Data::Dumper;

die "Usage: interleave-string a b c\n" unless @ARGV==3;
my( $a, $b, $c ) = @ARGV;

# could start with a prefix of $a, or a prefix of $b.

#
# my $isinterleaved = interleave( $a, $b, $c );
#	Return 1 iff $c is a result of interleaving variable length prefixes
#	of $a and $b, with the prefix of $a coming FIRST.  Return 0 otherwise.
#
fun interleave( $a, $b, $c )
{
	my $l = length($a);
	foreach my $i (1..$l)
	{
		my $pre = substr($a,0,$i);
		my $cpre = substr($c,0,$i);
		next unless $pre eq $cpre;
		my $arest = substr($a,$i);
		my $crest = substr($c,$i);
		say "debug: found possible prefix $pre (len $i) of $a and $c, leaving $arest, $b, $crest";
		return 1 if $crest eq "" || interleave( $b, $arest, $crest );
	}
	return 0;
}


my $isinterleaved = interleave( $a, $b, $c ) || interleave( $b, $a, $c );
say $isinterleaved;