Problem Statement
We want to make a worklist, specifying the order in which the cars should be moved. The order of cars in the worklist must allow each car to be moved just once all the way to its unloading position. How many different orders will allow this?
The cars are each named with a lowercase letter, and their destinations with the same letter but in uppercase. The positions of the cars and of their destinations are given by a sequence of letters that is regarded as circular by wrapping around the ends. So, for example, "BACacb" describes a situation in which going clockwise around the track we encounter B, A, C, a, c, b, and then return back to B. Here there are 3 different orders in which the cars can be moved to their destinations: a then c then b, a then b then c, or b then a then c.
Given the
original positions of the cars and destinations in a
Definition
- Class:
- CircleCount
- Method:
- countOrder
- Parameters:
- String
- Returns:
- int
- Method signature:
- int countOrder(String circle)
- (be sure your method is public)
Constraints
- circle will contain between 2 and 50 characters, inclusive.
- Each character will be a letter ('a'-'z', 'A'-'Z').
- No character will appear more than once in circle.
- If a letter appears in circle, it will appear both in uppercase and in lowercase.
Examples
"BACacb"
Returns: 3
This is the example given above.
"ABCacb"
Returns: 0
We cannot move c first. If we move a first, then we can never move b to B, but if we move b first we can never move a to A.
"xX"
Returns: 1
We must move car x. We could move it to its destination either in a clockwise or a counterclockwise direction but there is only one order for choosing the cars to move.
"ABCabc"
Returns: 2
The possibilities are 1) a then b then c, or 2) c then b then a (moving the cars in the other direction).
"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp"
Returns: -1
These 16 cars can be move in any order, so there are 16! orders which is greater than 2,000,000,000
"eAEBDbFdfcgCGa"
Returns: 210
"BACacbYXZxzy"
Returns: 0
//String circle = "BACacbYXZxzy"; //should be 0 //String circle = "BACacb"; //3 //String circle = "XYxPyp"; //1 //String circle = "pXYPxy"; // 2 //String circle = "pP"; // 1 //String circle = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP"; // -1 String circle = "ZYACBacbxzyX"; //20=10+10= [CBcbxzyXZY + [ACBacbxzXZ
"XYxPyp"
Returns: 1
"pXYPxy"
Returns: 2
"ZYACBacbxzyX"
Returns: 20
"abcdABCDefgEFGHIhJijKkLMlm"
Returns: 3603600
"Aa"
Returns: 1
"AazZyYBbcCdDgGiIOoPpEesS"
Returns: 479001600
"AazZyYBbcCdDgGiIOoPpEesSrR"
Returns: -1
"rRzZ"
Returns: 2
"gQGq"
Returns: 2
"TRrgGt"
Returns: 6
"hLHjlVJv"
Returns: 0
"vHWRhwVr"
Returns: 1
"nvayNVAY"
Returns: 2
"ZxXrnRzN"
Returns: 4
"ufeaEAUF"
Returns: 6
"qoeOEnNQ"
Returns: 12
"wWvVmMzZ"
Returns: 24
"dDzZRftFrT"
Returns: 0
"dnDocNOiCI"
Returns: 1
"DOocCspdSP"
Returns: 20
"pPoOLlBXbx"
Returns: 60
"mUuVBvbHMh"
Returns: 30
"GnNzvZiVgI"
Returns: 5
"XSOknKNxso"
Returns: 10
"xnKGDXNkgd"
Returns: 2
"lQqaAwWEeL"
Returns: 120
"LlkKeEqQyYwW"
Returns: 720
"kbjqBmJQpMzPZK"
Returns: 7
"dwoPQvnpqFDWOfVN"
Returns: 1
"gSpGyPYBbjJUuwWRrs"
Returns: 15120
"xwkWKPGZpDgHRzdhNrnX"
Returns: 360
"xSdvXDkwVKWlhLHEGJegjs"
Returns: 4620
"NGkbcESlVPMKjBCLJxhXHtiwuTIarzoWfUAdyRngeZsOFDvpYm"
Returns: 300
"utMdhOBlpnmobaevsAEfqVSFxQXiIYWCJZRGUTyDHwLPcjzNrg"
Returns: 8652600
"nSXgtEqcsxebBAaYOHRZULWVyMJPKDIohrzuNGlwvTmjQpkCdi"
Returns: 600
"YJNVLUDXsIMTSHhkKQEqewpcorbafWzyPjCnOvlRBudxAimtFZ"
Returns: 151800
"vZbNgznkKXYUIWMxyuLSDiwmlsdtTQEqPReHprFhCJfOcjVBGo"
Returns: 490314000
"DyKjYxJbXBFQEVOfqeGCvIMogUcAiWSPNmuawsLpnltTZzRrdk"
Returns: 1029659400
"ksFuxfqQoOHhpPiIzjZJMGTAmNCEVgLRBtaDYncevKlSrbUdXy"
Returns: 1211364000
"SmMKOWGkowPVgRDpQNvrLJUZdqnHBXljuzhbxtTaAEIeFiCfcs"
Returns: 1817046000
"huwaNHUysWAYSFEfGegIiJPTXQjZpCMOBLVtxqzDcmobRlvdrn"
Returns: -1
Total count is 5883768000.
"ZhNQKUHITYBiXtPyJOAbRxDSpjoWardswvmVMCcgelznGEqLku"
Returns: -1
Total count is 3432198000.
"UWTPjbfCZXyuwtKOVpcNzxkovMnmRrhHDdlLsSGgQqEIJBeFiY"
Returns: -1
Total count is 2422728000.
"QPwEdGjCsqpMUegIFcmuAifaxtXvobTyVrOBYRZzNnlLKWDkJS"
Returns: -1
Total count is 2353507200.
"DylGKpqMenRYLPQvENVXCAOHxZScaohzWswutUTBbijIdgJkmr"
Returns: -1
Total count is 2206413000.
"ntxTgXGdDyYzZQqjrJROoVNv"
Returns: 19958400
"eHhJjdDQRqXrxKkyYwWNCnczZE"
Returns: 518918400
"igGaAnNoOzZdDPpTtFfRrkjKJI"
Returns: -1
"HYQIAiaeEGgUulLFDZfdzMmNnhyq"
Returns: 1210809600
"yYmijpMhIJPHdDBbsSaekoAEKOQqlL"
Returns: 454053600
"jLMJDdYyOoNEnZAWezBRawbrufUvlFVm"
Returns: 5765760
"yXKOZNuYdjUafDJAFSEQHseqGhgtxkoTzn"
Returns: 6188
"phkyXuzxrvRVMmnNbgBGJjLlTQDtSOqCPdHsKYUoZc"
Returns: -1
Total count is 2051179200.
"GneklgCMcIZPmHiXzphxwouWOUrRsSVDBNvEKdbL"
Returns: 1995364800
"iVdvtTZznbNmphBjwMPHJWGgaAXRQUIxDrqu"
Returns: 252046080
"AounFlekafcgimCGIbMBZYzyhrHRWQOwUNqLEK"
Returns: 69837768
"DwcuzYgWmCUZGoMaOAIisSNFPXnfpVxvlbqLBQdy"
Returns: 931170240
"ADTVMlaFdtvNmfnPpsySiwYhqozIWHbQOkZBKERerL"
Returns: 174594420
"aNEQdfjXneqxbBVvpPhsrHmkSiyRMKIYGOUTgoADFutJ"
Returns: 465585120
"ZHxXEYKFTBRNeyVkIftbrnvPSWipOswoamAqMjQJlcLzCh"
Returns: 514829700
"rtyJBkXIlVdUHzmQajbxiCvuhqcWOSNwGoERTYKLDZsngeMA"
Returns: 1
"rFstfMKmPQUkDNLpqudnCZIlcziebEBhoaHjOAJxXYyVvgGRST"
Returns: -1
"RJjYLyZlEWzewagqAGkQKMSmsdDXxfFIiCOcobpBrP"
Returns: -1
"TWaZAOGoSgYsyqQMmdDrRxkXjpKJPBUbuvVlinfLIctNwzFC"
Returns: -1
"RFPIJUijuBbnvNhVHaASEGseYgyczCoZOlLDXdxwtqrWfpTQ"
Returns: -1
"TxhNtnwWZzEecCsrgSfRGFQBqbaAyYjilJILDdUuopOmPMKXHk"
Returns: -1
"LtjrOloNnpyPYKkwWGgHheEVvAUauxXmMDdfFZzcqCbQBTJR"
Returns: -1
"bcusSfmFMvVQqlkLKiIoOnNeEGgaAxXjJPpRrHYhTyDtdBCU"
Returns: -1
"rfXTJdAWBMcHRFDCOoknKyvigxtjNaYwbVmGhI"
Returns: 0
"IuDNUiydnY"
Returns: 0
"BxtjTKdXHGJDgvbkVh"
Returns: 0
"xcjhYUCyJDKIGLgQRPrqXdilHpuk"
Returns: 0
"rCubjdAvyzcaIOioEemMqQHXLSPKWNGhFxlspRkgwBJDnVYUZf"
Returns: 0
"ABCabcDEFdefGHZghz"
Returns: 1680
"mMuUgGiIsSrRcCaAoOkKbBqQvVwWeEpPZXhjzxHJtTdDlL"
Returns: 0
"aBAbcCdefDEFghijGHIJklKLPQpq"
Returns: 0
"bcaCBADdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYy"
Returns: 0
"ghijklMNOPQRSTUVXYZmnopqrstuvxyzABCDEFGHIJKLabcdef"
Returns: 5200300
"ABCabcDdEeFfGgHhIiJjKkLlMm"
Returns: 1037836800
"EeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuWwXxYyZzAbCaBcDd"
Returns: 0
"VXvxZYzyijIJaABbcCdDEefFgGhHKklLmMopOPqQrRsStuTU"
Returns: -1
"aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWyXxY"
Returns: 0
"AaBCbcdD"
Returns: 12
"ZzCcVvBbNnMmAaSsDdFfGgHhJjKkLlQqWwEeRrTtYyUuPOIpio"
Returns: 0