aboutsummaryrefslogtreecommitdiff
path: root/challenge-329/jeanluc2020/perl/ch-2.pl
blob: 865680766eb4b5ed5f5fec9fd317e0425bd9bf4c (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
#!/usr/bin/env perl
# https://theweeklychallenge.org/blog/perl-weekly-challenge-329/#TASK2
#
# Task 2: Nice String
# ===================
#
# You are given a string made up of lower and upper case English letters only.
#
# Write a script to return the longest substring of the give string which is
# nice. A string is nice if, for every letter of the alphabet that the string
# contains, it appears both in uppercase and lowercase.
#
## Example 1
##
## Input: $str = "YaaAho"
## Output: "aaA"
#
#
## Example 2
##
## Input: $str = "cC"
## Output: "cC"
#
#
## Example 3
##
## Input: $str = "A"
## Output: ""
##
## No nice string found.
#
############################################################
##
## discussion
##
############################################################
#
# For each character in $str, we use the lowercase character of it
# as the key in a hash. There we keep track of each character that
# we have seen so far whether it was in lower, upper or both cases.
# In the end, we walk the characters from $str again and just keep
# the ones for which we have seen both the lower and upper case
# variant.

use v5.36;

nice_string("YaaAho");
nice_string("cC");
nice_string("A");

sub nice_string($str) {
    say "Input: \"$str\"";
    my @chars = split //, $str;
    my $seen = {};
    my $result = "";
    foreach my $char (@chars) {
        if($char =~ m/[a-z]/) {
            $seen->{$char}->{lc} = 1;
        } else {
            $seen->{lc($char)}->{uc} = 1;
        }
    }
    foreach my $char (@chars) {
        if($seen->{lc($char)}->{lc} && $seen->{lc($char)}->{uc}) {
            $result .= $char;
        }
    }
    say "Output: \"$result\"";
}