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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
use v6d;
###############################################################################
=begin comment
Perl Weekly Challenge 099
=========================
Task #2
-------
*Unique Subsequence*
Submitted by: Mohammad S Anwar
You are given two strings $S and $T.
Write a script to find out count of different unique subsequences matching $T
without changing the position of characters.
*UPDATE: 2021-02-08 09:00AM (UK TIME) suggested by Jonas Berlin, missing entry
[5].*
Example 1:
Input: $S = "littleit', $T = 'lit'
Output: 5
1: [lit] tleit
2: [li] t [t] leit
3: [li] ttlei [t]
4: litt [l] e [it]
5: [l] ittle [it]
Example 2:
Input: $S = "london', $T = 'lon'
Output: 3
1: [lon] don
2: [lo] ndo [n]
3: [l] ond [on]
=end comment
###############################################################################
#--------------------------------------#
# Copyright © 2021 PerlMonk Athanasius #
#--------------------------------------#
#==============================================================================
=begin comment
Algorithm: Recursive search. The subroutine fine-subseqs() searches $S for the
first character in $T, and then recursively searches the remainder of $S for
the remainder of $T. The recursion ends when either $S or $T is exhausted. (In
the first case, no match was found; in the second case, a match has been found
and is recorded in the @subseqs array.)
Note: If $VERBOSE is set to True, details of all the different subsequences
found are displayed after the Output, as in the Examples. This is done using
the subroutine partition(), which inserts square brackets around those charac-
ters in $S which form a given subsequence.
=end comment
#==============================================================================
my Bool constant $VERBOSE = True;
#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
"\nChallenge 099, Task #2: Unique Subsequence (Raku)\n".put;
}
#==============================================================================
sub MAIN
(
Str:D $S where { $S.chars > 0 }, #= Non-empty string to be searched
Str:D $T where { $S.chars > 0 } #= Non-empty substring to search for in S
)
#==============================================================================
{
qq[Input: \$S = "$S", \$T = "$T"].put;
my Array[UInt] @subseqs;
my UInt $subseqs = 0;
if $T.chars <= $S.chars # No match is possible if $T is longer than $S
{
find-subseqs( $S, 0, $T, @subseqs );
$subseqs = @subseqs.elems;
}
"Output: $subseqs".put;
if $VERBOSE && $subseqs > 0
{
my UInt $count = 0;
"\n".print;
" %d: %s\n".printf: ++$count, partition( $S, $_ ) for @subseqs;
}
}
#------------------------------------------------------------------------------
sub find-subseqs # Recursive subroutine
(
Str:D $s, #= String to be searched
UInt:D $i, #= Start index for search
Str:D $t, #= Substring to search for
Array:D[Array:D[UInt:D]] $subseqs #= Subsequences found
)
#------------------------------------------------------------------------------
{
state UInt @seq;
# (1) Separate the search substring into $t0, its first character, and
# $t-rem, the remaining characters (if any)
$t ~~ / ^ (.) (.*) $ /;
my Str ($t0, $t-rem) = $/.map: { .Str };
# (2) Beginning with the character at index $i, check each remaining
# character in $s as a possible match for $t0: if it matches, add the
# character's index ($j) to the array @seq and recursively search the
# remainder of $s for the substring $t-rem
for $i .. $s.chars - 1 -> UInt $j
{
if $t0 eq $s.substr: $j, 1
{
@seq.push: $j; # Add index $j to the sequence
if $t-rem.chars == 0
{
$subseqs.push: @seq.clone; # Copy the sequence and record it
}
else
{
find-subseqs( $s, $j + 1, $t-rem, $subseqs ); # Recursive call
}
@seq.pop; # Remove index $j from the sequence
}
}
}
#------------------------------------------------------------------------------
sub partition( Str:D $S, Array:D[UInt:D] $seq --> Str:D )
#------------------------------------------------------------------------------
{
my Str @partition = $S.split: '', :skip-empty;
# (1) Add square brackets around each character in the subsequence
for @$seq -> UInt $i
{
@partition[ $i ] = ' [' ~ @partition[ $i ] ~ '] ';
}
# (2) Remove initial and trailing spaces, and brackets and spaces internal
# to a sequence of contiguous characters. E.g., " [l] [i] [t] tleit"
# becomes "[lit] tleit"
my Str $partition = @partition.join: '';
$partition ~~ s :g / \] \s ** 2 \[ //; # Remove internal brackets & spaces
$partition ~~ s / ^ \s //; # Remove initial space
$partition ~~ s / \s $ //; # Remove trailing space
return $partition;
}
#------------------------------------------------------------------------------
sub USAGE()
#------------------------------------------------------------------------------
{
my Str $usage = $*USAGE;
$usage ~~ s/ ($*PROGRAM-NAME) /raku $0/;
$usage.put;
}
##############################################################################
|