Problem Statement
TopPilot airlines is committed to customer service and convenience. Thus, it is not simply enough to have a flight from San Francisco, CA to Hartford, CT. There must be a direct flight.
You will be given a
Write a method that, given these one-way flight paths, adds direct one-way flights between airports that are currently connected by non-direct one-way flights. For example, if there is a direct flight from SFO to JFK and a direct flight from JFK to LAX, and not a direct flight from SFO to LAX, then add a direct flight from SFO to LAX. Do not, however, add a direct flight from an airport to itself, even if a loop exists. Once this is done, the method should return the total number of flights provided by TopPilot airlines.
Definition
- Class:
- TopPilot
- Method:
- addFlights
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int addFlights(String[] destinations)
- (be sure your method is public)
Notes
- If the destination airport does not appear as an element in destinations (which is always the case when it is greater than 19), there are no departing flights from that airport.
- Leading zeroes in the numbers are allowed.
Constraints
- No airport will have a direct flight to itself.
- destinations will have between 1 and 20 elements, inclusive.
- each element of destinations will contain only the characters '0' through '9', and commas ','.
- each element of destinations will be a comma delimited list of integers, with no numbers repeated, and no leading, trailing, or extra commas.
- each element of destinations will be contain between 1 and 50 characters, inclusive.
- destination airport numbers will be between 0 and 50, inclusive.
Examples
{"1","2","3"}
Returns: 6
There is a flight from airport 0 to airport 1, from 1 to 2, and from 2 to 3. We add a direct flight from 0 to 2, from 1 to 3, and from 0 to 3 (note that 3 is not an element, so it has no departing flights).
{"1","02","3","4,5"}
Returns: 14
We add flights from 0 to 2, 3, 4, and 5. We add flights from 1 to 3, 4, and 5. We add flights from 2 to 4 and 5. Together with the 5 flights we already have, there are 14 total.
{"1,2","0,2","1"}
Returns: 6
The only added flight is from 2 to 0.
{"1,5,7,8,13,15,16,23,26,35,32,41" ,"0,2,5,6,7,15,18,21,25,31,34,41,49,50" ,"5,10,15,20,25,30,35,40,45,50"}
Returns: 62
{"40,6,38,5,12","20,29,6,50,5,14,0","22,39,14,13,9,31,26,49,1,23","19,30,29,37,36,46,24,0","42,49","43,19,48,2,13,33,0","43,20,8,48","1","14,34","21,44,19,17,15,14,33,31,7,5,27,50,48,1,23","20,41,16,15,38,14,36,35,32,8,31,29,27,24","32,49,5,26,13,24,35,34","22,44,20,17,40,38,35,11,10,32,30,29,5,50,47,23","15,14,37,12,11,32,49,48,0","34","21,16,14,33","41,39,15,14,37,12,11,10,32,8,6,27,3,24,1","21,20,41,40,15,33,9,7,4,27,26,48,46","22,15,38,10,9,6,4,27,23","21,42,10,9,7,6,50,2,47"}
Returns: 748
{"1","2","0"}
Returns: 6
{"18,48","27","21,7,18,50,17,35,12,34","32,31,19,41,36,23","42,40,16,35,34,33,9,32,8,7,27,26,25,1","45,18,17,39,15,37,11,34,10,32,30,29,4,27,23","30,38,36,46,45,44,22","44,17,16,15,14,12,34,10,9,8,29,50,24","22,21,41,39,38,35,10,29,5,50,26,2,24","1","2","45,21,44,42,38,15,36,32,9,8,28,27,49,1,24,46,23","10,39","22,42,18,39,37,34,33,9,4,1,47,0,46","3","44,18,16,38,35,7,29,27,0","4","30,14,47,13,22","21,41,49,47,24,22","22,42,18,16,37,12,35,34,33,9,7,26,23,0"}
Returns: 729
{"40,39,35,34,10,32,7,6,29,5,27,26","7,37,36,24,12,45","43,19,18,39,37,33,8,28,50,4,48","22,21,41,16,39,15,30,29,50,26,46","45,20,18,16,39,15,37,12,11,34,8,29,5,27,26,48,24","42,38,15,36,24,35,45","21,12,34","22,44,21,19,36,32,31,6,5,50,26,1,23,46","28,38","32,8,30,18,24,13","45,22,21,20,15,36,12,34,32,31,5,4,24","27,34","22,21,42,18,17,13,33,9,32,8,6,28,4,27,50,3,26","8,30,7,39,24,34","45,21,41,18,16,35,34,11,10,8,7,3","45,21,43,17,36,11,9,28,26,49,48,1","21,40,17,38,33,10,9,6,2","45,44,20,41,38,14,12,9,8,7,6,2,46,0","30,7,50,14","43,39,14,36,33,9,7,2,24,23"}
Returns: 868
{"43,16,39,14,13,12,35,32,28,3,24,47","32,9,38,48,37,47,3,36","44,41,17,38,37,12,34,11,10,27","20,43,42,41,37,31,29,6,24,1,23","45,19,41,18,39,34,10,9,32,27,50,0","38,27,3,24","22,21,17,14,35,31,28,4,27,26,24,47,0","45,18,17,14,12,10,8,30,4,2,24","1","32","45,42,41,16,39,38,12,11,33,5,50,48,46,0","21,42,17,38,36,12,32,8,30,27,4,50,2,24,47","43,17","39","2","21,41,16,38,14,10,32,7,6,25,47,0","3","45,44,21,43,41,12,34,11,10,32,31,30,27,24,47","32,41,7,35,13,12,0","43,18,39,15,36,11,34,33,10,9,8,27,1,0,23"}
Returns: 866
{"41,28,50,24,13","42,41,27,48,2,34,23,11","22,44,21,40,33,25,24,47,0","20,11,9,30,28,4,27,1,24,47","9,18,50,6,46,35,1,0","50,49,38,35,24,1,44","44,19,15,38,13,35,9,8,49,46","19,29,26,3,2,11","45,17,40,39,15,37,14,36,31,29,5,50,24,46","45,21,19,42,18,39,33,6,26,49,25,0","43,39,16,13,1","45,21,19,35,10,7,30,5,48,1,46","44,20,42,15,14,35,33,9,29,28,3,26,49,25","39,28,6,4,25,36,44","31,30,2,13,44","17,6,38,16,24,33","32,8,29,27,5,37,14,46","21,18,39,50","22,17,39,13,36,11,27,3,1,24","45,42,18,36,12,34,33,6,50,26,2,1,0,46"}
Returns: 1000
{"44,19,18,41,38,36,35,7,6,50","32,9,47,25,35,2,44","44,40,16,14,34,32,6,49,0","21,6,15,4,46,24,1,0","43,9,32,31,30,29,5,49,26,48,25,47,0","41,37,13,33,10,7,29,6,49,2,25,1","44,21,16,15,37,33,8,27,26,2,24,1,47,0","45,43,18,15,14,9,8,6,29,28,4,26,25,1,46","22,44,39,15,37,33,10,48,47,24","45,21,42,15,13,36,35,34,10,8,7,6,5,26,25,1","45,19,41,15,38,31,28,26,49,24,0","45,22,19,17,15,36,10,28,1","44,18,40,16,14,11,32,9,29,50,49,2,47","41,29,18,39,16,26,14,24","32,30,5,2","17,39,38,13,35,49,48,24,23","21,43,42,11,32,7,29,5,50","10,31,40,18,28,16,44","21,31,30,16,4,2","45,44,42,18,17,15,37,13,8,7,29,4,1,46"}
Returns: 942
{"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","0"}
Returns: 380
{"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18,41,42,43,44,45,46,47,48,49,50","19,31,32,33,34,35,36,37,38,39,40","0,20,21,22,23,24,25,26,27,28,29,30"}
Returns: 1000
{"20,4,24,2,11","20,42,17,33,31,8,7,30,6,49,25","10,20,17,13,0","45,43,41,39,14,13,9,31,0","43,42,7,49,5,3,12,44","7,49"}
Returns: 101
{"21,35,34,9,30,2,48,1,46","18,17,28,37,13","20,41,17,38,36,12,33,28,4,27,26,49,24,47"}
Returns: 45
{"21,20,43,18,17,35,9,29,50,1,46","21,42,41,17,8,7,28,27,48,47","10,24,11","20,9,40,18,27,47,33","38,37,25,2,23","21,43,16,38,37,36,33,8,31,7,29,49,2","44,42,40,15,38,14,36,11,33,9,28,27,2,25,0"}
Returns: 95
{"44,43,17,38,14,35,8,27,50,49,25,2,47,1","21,41,18,5,26,2,11","10,30,35","20,40,39,38,12,10,7,29,6,5,49,25,0,23,46","43,19,41,18,39,13,35,10,29,5,50,3,2,1,46,23","44,43,19,39,34,11,33,32,8,30,27,3,26,2,48","1","20,30,6,39,25,1","50","44,43,17,16,37,14,34,33,8,6,28,2","42,39,37,12,8,3,48,1,0","40,44","43,17,37,31,5,28,27,47,46","22,45,21,41,18,17,37,36,10,30,1","21,30,7,37,3,35,12","45,22,20,16,35,11,7,28,4,50,24,23,0","21,43,19,39,38,15,37,36,12,34,9,31,28,26,25,23","39,16,25,24,13,23,34","43,40,27,15,23,11"}
Returns: 853
{"19,39,38,12,34,10,32,29,28,27,49,26,3,48,25,2,47","21,13,11,33,9,31,7,28,3,48,0,46","21,43,40,38,37,11,8,30,6,29,5,4,49,23,46","22,43,40,8,5,26,47,46,23","10,8,18,50,38,36,24,12","20,30,19","49,15,4,23,0,33"}
Returns: 176
{"40,6,5,37,26,34","45,21,42,18,41,40,38,31,7,29,28,25","45,44,40,17,37,13,12,11,34,33,31,6,48,1,0,23","44,42,37,36,11,9,29,28,27,26,23,46","21,44,20,38,14,35,6,27,25,0","15","22,44,42,18,37,36,27,49,47,0,46","20,19,40,17,16,39,14,37,13,31,28,49,3,48,1,23,46","44,43,18,17,38,35,11,10,9,31,30,50,49,2,0","44,20,40,14,36,33,30,29,28","22,45,19,40,16,14,35,9,31,5,50,3,23","42,40,14,36,12,35,34,9,6,25","45,20,17,16,31,30,6,50,26,48,1,24","20,43,41,40,16,15,28,4,2,24,0","42,38,32,8,27,3,26,24,46,23","45,44,38,14,33,9,31,30,27,3,49,2,24,46"}
Returns: 800
{"45,43,42,15,38,37,36,29,6,5,49,3,48,1,23","19,28,17,48,25,24,35,22","20,4,25,36,13","10,41,30,27,48,1,33,0","21,25,1","18,2,12","16,13,35,10,32,31,8,5,4,3,2,48,1,47","43,32,5,38,26,45,11,44","29,7,18,6,4,46,22","25,34","22,19,14,13,31,8,30,27,25,48","43,19,16,14,10,9,30,6,26,48,1,23,46","22,42,39,15,38,14,13,10,9,32,7,27,48,1,24,0","45,8,30,4,50,3,24,0,23","32,31,19,29,5,37,36,34","22,44,21,43,18,36,12,34,31,28,27,50,3","22,19,18,38,37,9,29,5,27,26,0","21","19,17,39,10,31,6,27,4,3,25,0","42,40,17,37,3,14,35"}
Returns: 903
{"19,42,38,14,13,34,11,9,32,7,5,4,24,1"}
Returns: 14
{"22,20,43,40,16,15,14,12,10,32,5,4,26,46","44","20,40,10,9,32,27,49,25,48","10,41,36,13,0","1","21,18,16,15,8,31,7,3,1","21,10,18,48,37,36,35,45","20,19,16,37,13,31,27,3,0","22,18,14,9,31,29,28,5,50,49,25,24,23","2","43,18","20,43,19,17,16,39,35,33,31,29,4,26,49,1,0","19,17,47,3,46,45","21,17,28,39,27,15,34,1","44,41,11,6,27,3,1,24,0"}
Returns: 421
{"20,43,19,41,17,36,12,34,31,5,4,27,24,47","41","22,44,41,10,33,8,7,30,28,5,24","41,16,37,32,7,26,48,25,23","22,21,44,43,17,16,38,9,8,27,3,48,25,24","45,42,40,16,15,37,35,6,46","26,1,11","20,31,48,3,25,1,45,11","31,39","44,20,42,16,13,35,12,10,5,28,50,1,23"}
Returns: 182
{"45,44,43,20,19,41,17,15,37,14,8,6,25,1,24","42,19,50,17,27,2,46,22","42,16,23,44","22,44,20,42,16,38,36,10,30,7,5,27,26,49","8,29,18,37,2,44","42,30,39,15,26,4","32,4,25,14,13,23"}
Returns: 103
{"45,21,44,43,14,12,11,9,7,29,27,50,25,48,2,47,23","31,40,28,34,45,0","21,9,37,24,1,12,11","41,18,6,38,25,23,1","22,43,42,18,40,17,12,8,31,50,3,25,48,1,23","42,39,15,37,12,10,32,29,4,27,26,1,24","5,27,35,13,45,34","21,43,18,38,14,34,33,32,8,31,30,6,3,49","30","32,8,49,0","43,15,38,36,12,34,9,8,28,50,48,24,0","44,21,18,17,15,14,36,10,7,48","32,9,19,50","22,45,18,17,39,38,36,11,10,30,6,5,26","45,19,42,37,36,10,30,50,26,2,24,0,46","10,28,27,26,14,1,34"}
Returns: 721
{"32,18,17,15,23","22,44,17,13,12,32,9,8,31,6,5,28,4,2","21,43,19,18,14,12,32,7,29,50,26,25,48,24,23","18,16,15,13,34,11,10,31,8,29,5,28,27,0"}
Returns: 64
{"21,20,40,39,14,35,33,32,31,7,6,5,4,27,3,24","21,44,19,18,41,15,50,49,47","36,12","45,43,19,39,13,12,35,34,33,50,4,48,25,23","6,38,49,24,46,0","44,43,12,32,30,6,50,25,24","4","45,44,41,17,39,15,13,12,10,8,31,30,29,26,1","50","21,20,18,38,37,13,33,8,7,25,24,47","43,18,13,8,31,6,4,27,25,48,1","38"}
Returns: 343
{"20,16,36,5,3,26,48,2,1,24,47","25","29,18,26,0","43,42,10,33,32,31,28,4,26,48,46","22,20,42,16,35,11,34,33,8,31,50,49,3,25","9,31,41,38,37,14,3,11","20,42,8,2,1,33","10,17,49,4,26,45,44","22,39,13,12,11,4,50,26,0","45,21,42,18,16,36,13,35,33,29,26,2,25,47,46","41,17,16,12,11,28,27,26,2,48"}
Returns: 424
{"29","44,19,17,16,14,37,35,34,32,5,3,49,2,25,47"}
Returns: 16
{"45,43,20,17,16,38,12,11,7,29,6,50,26,46","50,6,39,48,13","6,5,25,3","22,42,17,40,39,15,14,13,6,47,1","32","1","38,11","18","43,21,18,48","21,10,20,15,2,12,22,0","20,41,17,12,35,32,31,5,4,50,47,1,24,23","22,44,20,18,39,15,34,8,7,29,28,49,0"}
Returns: 284