SerialNumbers
SRM 366 · 2007-09-18 · by jthread
Problem Statement
You own a lot of guitars, and each guitar has a unique serial number. You want to be able to look up serial numbers quickly, so you decide to sort the entire list as follows.
Each serial number consists of uppercase letters ('A' - 'Z') and digits ('0' - '9'). To see if serial number A comes before serial number B, use the following steps:
- If A and B have a different length, the one with the shortest length comes first.
- Else if sum_of_digits(A) differs from sum_of_digits(B) (where sum_of_digits(X) returns the sum of all digits in string X), the one with the lowest sum comes first.
- Else compare them alphabetically, where digits come before letters.
Given a
Constraints
- serialNumbers will contain between 1 and 50 elements, inclusive.
- Each element of serialNumbers will contain between 1 and 50 characters, inclusive.
- serialNumbers will only contain uppercase letters ('A' - 'Z') and digits ('0' - '9').
- All elements of serialNumbers will be distinct.
{"ABCD","145C","A","A910","Z321"}
Returns: {"A", "ABCD", "Z321", "145C", "A910" }
The first serial is "A" because it has the shortest length. All others have length 4, but "ABCD" has the lowest sum. Next lowest is "Z321", and finally "A910" comes before "145C" because "A" comes before the "1" (they both have sum = 10)
{"Z19", "Z20"}
Returns: {"Z20", "Z19" }
1+9 > 2+0, so "Z20" comes first.
{"34H2BJS6N","PIM12MD7RCOLWW09","PYF1J14TF","FIPJOTEA5"}
Returns: {"FIPJOTEA5", "PYF1J14TF", "34H2BJS6N", "PIM12MD7RCOLWW09" }
{"66","MF5CM7M8151FD1ZGPJQWQ2WQUI2","656OZULF6SKI5E","86JWL6D20SIK6AIM5H7S4UV6L0BVE","MU4I5Z2MW6NXI5KRKTZEHJE","IOHLO8BW","TY289WOG7HH76W3GM7OWRRCJCXKDQJG4MFW13H2V2RE3VK","19MBHGOA540N8DOMRHJYLLE3Q51TCIQ9QQIQ49ADT3GFFZUH1","2Y59Y9MC1FIRXH9TR5KQJQD34TS9ORHENEPWHB6FZNCFUOR","DEKY71BOTFB4Y9GVNXWP9HTJSJYGIF2X9UB5A5KAECTBAM5WQ","0QGU111760J1A69I9SPD6Q065401OTF08HI81JNJB5T7","SAIVW9QJRQ97NGLLAZTEUI1","681WA9QQNWU","JMKO0B9OZDNI0GJR75IB7HVWBYMOR297JWETBAROVSC0P5XXVL","MTUC1SGAQV3","MJ72EY8FQHQQTB71EPVRF2GI","BF5ZAWLOCKY1FD65LNT8KI9ZZB","RDA1VDGCC3KVIB4QVCCPHRTYSQQLPLG9DXOB334VPE","IB","H5COB8A5YJ","GSBTGVOYHY","1OETM21115C2T39J31FZG6ER83O3OIC61Q5UYFIKD2JWUNH6","94QCI0BXIWZUG7ZSZ44ZLYNVY18JQIZX7AS02VW","GBEX7G2K8V7RN74KUFI2A2WROYEZDGAGQGLS299HO5EKBIBFPH","J6FU0J1T92Z8TUWJ3NX","85L8QVMILS9","6U059VKLRKH8KYU36937X2HEP7ILA6ZMNC4UG","000LP2VWS4XE9D8"}
Returns: {"IB", "66", "IOHLO8BW", "GSBTGVOYHY", "H5COB8A5YJ", "MTUC1SGAQV3", "681WA9QQNWU", "85L8QVMILS9", "656OZULF6SKI5E", "000LP2VWS4XE9D8", "J6FU0J1T92Z8TUWJ3NX", "MU4I5Z2MW6NXI5KRKTZEHJE", "SAIVW9QJRQ97NGLLAZTEUI1", "MJ72EY8FQHQQTB71EPVRF2GI", "BF5ZAWLOCKY1FD65LNT8KI9ZZB", "MF5CM7M8151FD1ZGPJQWQ2WQUI2", "86JWL6D20SIK6AIM5H7S4UV6L0BVE", "6U059VKLRKH8KYU36937X2HEP7ILA6ZMNC4UG", "94QCI0BXIWZUG7ZSZ44ZLYNVY18JQIZX7AS02VW", "RDA1VDGCC3KVIB4QVCCPHRTYSQQLPLG9DXOB334VPE", "0QGU111760J1A69I9SPD6Q065401OTF08HI81JNJB5T7", "TY289WOG7HH76W3GM7OWRRCJCXKDQJG4MFW13H2V2RE3VK", "2Y59Y9MC1FIRXH9TR5KQJQD34TS9ORHENEPWHB6FZNCFUOR", "1OETM21115C2T39J31FZG6ER83O3OIC61Q5UYFIKD2JWUNH6", "DEKY71BOTFB4Y9GVNXWP9HTJSJYGIF2X9UB5A5KAECTBAM5WQ", "19MBHGOA540N8DOMRHJYLLE3Q51TCIQ9QQIQ49ADT3GFFZUH1", "JMKO0B9OZDNI0GJR75IB7HVWBYMOR297JWETBAROVSC0P5XXVL", "GBEX7G2K8V7RN74KUFI2A2WROYEZDGAGQGLS299HO5EKBIBFPH" }
{"MA8U3HEPWMD0NLL8J1CMAWQM10MCVSWJZ8E98KO","EMS9RVG72ILZXWBXDFKNUUSKJ2QFI4XS8ACAASM","Q9A8TZ6F0675X36400RKB","KUFIG5MS97KT6MORNU","8ISQV33","GZAUF2TT5HL9XFJ3FYKRKDWVU38WIMAMAIHE411ZX6FS","11779BPQUJ0YGS1Z82BVSMPYL9UI915KS1AHHPK4BKVZ4LBT","7D0XBP5PR1QG76WN5RL7F300CNQ","2U67VK8UWFA4QU"}
Returns: {"8ISQV33", "2U67VK8UWFA4QU", "KUFIG5MS97KT6MORNU", "Q9A8TZ6F0675X36400RKB", "7D0XBP5PR1QG76WN5RL7F300CNQ", "EMS9RVG72ILZXWBXDFKNUUSKJ2QFI4XS8ACAASM", "MA8U3HEPWMD0NLL8J1CMAWQM10MCVSWJZ8E98KO", "GZAUF2TT5HL9XFJ3FYKRKDWVU38WIMAMAIHE411ZX6FS", "11779BPQUJ0YGS1Z82BVSMPYL9UI915KS1AHHPK4BKVZ4LBT" }
Submissions are judged against all 77 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class SerialNumbers with a public method vector<string> sortSerials(vector<string> serialNumbers) · 77 test cases · 2 s / 256 MB per case