PROBLEM STATEMENT
We have a battery connected at points A and B to a network of resistors. But we
suspect that the network has redundant resistors. A resistor is redundant if
there is no simple path from A to B that goes through it. A simple path is one
which has no repeated connection point (since current cannot flow through a
circular section of a path). Your program will be given the network and will
return a list of all the resistors that are redundant.
DEFINITION
Class Name: Resistor
Method Name: noPath
Parameters: String
Returns: String[]
Method Signature (be sure your method is public): String[] noPath(String
network);
Connection points are represented by capital letters. The battery is connected
at A and B. Every pair of adjacent characters in network represents a resistor
that goes between those 2 connection points. You should return a list of all
the redundant resistors in alphabetic order, with each resistor named by its
two connection points in alphabetic order.
Example: "ABCDBEBFA" is a network with 8 resistors: AB,BC,CD,BD,BE,BE,BF,AF
The resistors from B to E and then from E to B are parallel resistors. They
are both named BE since B precedes E in the alphabet. The network (not showing
both parallel resistors) is shown:
C--D
| /
|/
A---B---E
\ |
\ |
F
The simple paths AFB and AB show that resistors AB,AF,BF are not redundant,
but none of the others is contained on a simple path between A and B. For
example, BC is contained in the path ABCDB but this is not a simple path, since
it contains the loop BCDB. So the proper return is {"BC","BD","BE","BE","CD"}
NOTES:
- a resistor with both ends connected at the same point is redundant
- each string in the return must be two letters in alphabetic order
- the strings in the return must be in alphabetic order
- parallel redundant resistors must all appear in the returned collection
TopCoder will ensure the validity of the inputs. Inputs are valid if all of the
following criteria are met:
- network contains only upper case letters ('A'-'Z')
- network contains between 2 and 30 letters inclusive
EXAMPLES
(quotes shown for clarity only)
1) "AXBAB": return { }
None of these four resistors is redundant.
2) "AXYZA": return {"AX","AZ","XY","YZ"}
B doesn't appear, so all are redundant.
3) "XBAA": return {"AA","BX"}
4) "ARTRABTXT": return {"TX","TX"}
5) "ABCADEAFGHBCHDEHFEDFCBFAEBCEDC": return { }