Initial Analysis
- Two (2)
keys are
special:
- silly:
- quiet:
- Typing silly
outputs nothing
|
|
- Additional
input
requirements:
- silly appears
in the input
at least once
- quiet may or may not
be found in the input
- The sequence
"silly
quiet"
is not in
input
- The sequence
"quiet
silly"
is not in
input
|
|
Step 1: use the first
mismatched character
to find
quiet
or
silly
- You can find
silly or
quiet using the
first
mismatched
character:
- The first
mismatched character is
silly and
sillyOut if:
input: ---------- x x x x ... y
same
output: ---------- z z z z ...
Then:
silly = x
sillyOut = z
|
- The first
mismatched character is
quiet if:
input: ---------- x x x x ... y
same
output: ---------- y
Then:
quiet = x
|
|
|
Step 2a: silly was
found in step 1,
find quiet
- If first
mismatched
character was
silly,
then find
quiet as follows:
- If
length(input) = length(output):
quiet =
-
- Otherwise:
I = {input characters} - silly
O = {output characters} - sillyOut
quiet = I - O
|
|
|
Step 2b: quiet was
found in step 1,
find silly
- If first
mismatched
character was
quiet,
then find
silly and
sillyOut
as follows:
I = {input characters} - quiet
O = {output characters}
silly = I - O
sillyOut = O - I
|
|
Solution in Python
inp = input() # Typed line
outp = input() # Output line
# Find first different character in inp and outp
for i in range( len(inp) ):
if ( inp[i] != outp[i] ):
print(f"First diff char: {inp[i]} != {outp[i]}")
firstDiffCharInOutp = outp[i]
print(f"firstDiffCharInOutp = {firstDiffCharInOutp}")
break
# Find next different char in input
for j in range(i+1, len(inp)):
if (inp[j] != inp[i]):
break;
print(f"Next diff char in input = {inp[j]}")
# Find first special character (quiet or silly)
if ( firstDiffCharInOutp == inp[j] ):
# We found "quiet"
quiet = inp[i]
print(f"** quiet = {quiet}")
silly = None
sillyOut = None
else:
# We found "silly"
silly = inp[i]
print(f"** silly = {silly}")
sillyOut = firstDiffCharInOutp
quiet = None
# Now find the 2nd special character
inpSet = set(inp)
outpSet = set(outp)
if ( silly == None ):
# Find silly
print(f"Find silly....")
inpSet.remove(quiet) # Remove quiet from inpSet
diff1 = inpSet.difference(outpSet)
silly = list(diff1)[0]
# Find sillyOut
diff2 = outpSet.difference(inpSet)
sillyOut = list(diff2)[0]
else:
# Find quiet
if ( len(inp) == len(outp) ):
quiet = '-'
else:
diff1 = inpSet.difference(outpSet)
quiet = list(diff1)[0]
print(silly, sillyOut)
print(quiet)
|
Solution in C++
String inp = first line of input
String outp = second line in input
// Find first different character in inp and outp
for ( int i = 0; i < length(inp); i++ )
{
if ( inp[i] != outp[i] )
{
firstDiffCharInOutp = outp[i];
break;
}
}
// Find next different char in input
for ( j = i+1; j < length(inp); j++ )
if ( inp[j] != inp[i] )
break;
// Find first special character (quiet or silly)
if ( firstDiffCharInOutp == inp[j] )
{ // We found "quiet"
quiet = inp[i];
silly = '\0';
sillyOut = '\0';
}
else
{ // We found "silly"
silly = inp[i];
sillyOut = firstDiffCharInOutp;
quiet = '\0';
}
// Now find the 2nd special character
inpSet = set(inp); // Compute the set of letter in inp
outpSet = set(outp); // Compute the set of letter in outp
if ( silly == '\0' )
{
inpSet = inpSet - {quiet}; // Remove quiet from inpSet
diff1 = inpSet - outpSet;
silly = diff1[0];
}
else
{
if ( length(inp) == length(outp) )
quiet = '-';
else
{
diff1 = inpSet - outpSet;
quiet = diff1[0];
}
}
print(silly, sillyOut);
print(quiet);
|
❮
❯