Problem Statement
Jakub is trying out a one-dimensional keyboard. It consists of a single row of keys. The distance between any two adjacent keys is 1. Each key contains a distinct letter of the English alphabet. Jakub uses only one finger to type on the keyboard. He wonders what is the smallest total distance he will have to move his finger while typing a given word.
For example, if the keyboard's only row is "qwertyuiop", and Jakub wants to type the word "potter", he will have to move his finger from 'p' to 'o' (distance 1), from 'o' to 't' (distance 4), from 't' to 't' (distance 0), from 't' to 'e' (distance 2) and from 'e' to 'r' (distance 1), for a total distance of 1 + 4 + 0 + 2 + 1 = 8.
You are given a
Definition
- Class:
- TypingDistance
- Method:
- minDistance
- Parameters:
- String, String
- Returns:
- int
- Method signature:
- int minDistance(String keyboard, String word)
- (be sure your method is public)
Notes
- When moving the finger from the i-th key to the j-th key, the distance covered by the move can be computed as |i-j|, that is, the positive difference between i and j.
Constraints
- keyboard will contain between 1 and 26 characters, inclusive.
- Each character in keyboard will be a different lowercase letter of the English alphabet ('a'-'z').
- word will contain between 1 and 50 characters, inclusive.
- Each character in word will be present in keyboard.
Examples
"qwertyuiop"
"potter"
Returns: 8
The example from the problem statement.
"tc"
"tctcttccctccccttc"
Returns: 9
"a"
"aaaaaaaaaaa"
Returns: 0
"kwadrutove"
"rowerowe"
Returns: 39
"qwertyuiopasdfghjklzxcvbnm"
"topcodersingleroundmatchgoodluckhavefun"
Returns: 322
"d"
"dddddddddddddddddddddddddddddddddddddddddddddddddd"
Returns: 0
"co"
"coccooccocoococoocccoo"
Returns: 13
"vej"
"ejevvevevjjjejvevvvevvejeeeejjjejjveevjjvjejevveve"
Returns: 40
"lsit"
"sltittsltiitststsitlltitltiilsillltiliitiss"
Returns: 58
"xkumo"
"xmmxuuuxmxooxxumkxokuxokkxkmoxxkkumukkxkxkkmxmxoom"
Returns: 81
"tqlbdo"
"oboq"
Returns: 8
"ecjskyi"
"eesjjyykeysccsseyskkejeeyciikkcesejjeejysyycceyies"
Returns: 101
"awcyepqf"
"ppfywwyceayfyaacaypfyecpcqyywfafqyp"
Returns: 90
"kdqtbmnep"
"bpnkdqtdnq"
Returns: 26
"vhtgywqkem"
"wvkkvwqqtqetevqqmhqmvmythemmvhtmtwvkhqvvgegekmhkwy"
Returns: 209
"tqrgnikvyxj"
"gjtynnynyviitjyikgxrgjivyir"
Returns: 103
"cahvkibdmejt"
"vhejijvabvimajedbhtackvabvmkitjceatchtjbvtijbeckvi"
Returns: 226
"kfsqvzpwbtmhg"
"qqphwmzzkbpmvbhggbfzpkwwbbfvwggkhkzwwvfvvmhpsspwtf"
Returns: 182
"izkldmubhqonrg"
"zzoimlbgrgdkhirdgdigbl"
Returns: 128
"qilwphgasdcfvun"
"hcpflldugvslficifnvvwahcuqhiacpsgispvvvqdgwlavcwsp"
Returns: 259
"efsxwdybtivcpurk"
"yidkxkbifcvbbtpkfyyyrxvdpurcfssbepibcykyxwrwpcb"
Returns: 247
"sipndlwvyzcxtemkg"
"ntekzwvgxyewygenltgvmssvgmcnytcelcnvmdxexlnglypykw"
Returns: 275
"weiuosvgkhtbjdymrx"
"drbdetjgitumrduhhdwdshohxeehtyewjrytwy"
Returns: 239
"uatspmolvizqnhryedg"
"vqgaahssnnmeepmuqevhdhovooqeezeqszinpvahooazganumt"
Returns: 296
"phjafsgzoirxqevkcumy"
"earxykijjrcjcfryygucfh"
Returns: 152
"zdpvagwsouileyjnfqrcm"
"alqnueweyiqsnszzczagdoiuquivpugrvenszjccicvqzrlnaa"
Returns: 352
"damgreuvtjiqswkfxzopbc"
"tticddvdizidsfqjgmicwwqojjisofsrpvwggmzrazsfofpffs"
Returns: 294
"pnmjsqldkwfhoyxrzibvtug"
"mnkqjwbqbptdzinxzjrytrfuqvsvdqpskglxqbyrtixgqdkvfz"
Returns: 420
"tfqvbawsdeuypcgxmirlokjz"
"zayapdleckjikgoflvwykzktkdpwsmrc"
Returns: 254
"ijzqafksuobcmlypgthrdwnex"
"obyhorcajwymhsfotmhyqodtm"
Returns: 157
"ljkhuogfrqwycvabszxptiendm"
"feiusvgkvwdocbciqktviclbsgvzdonuzmlktyybolrjxynilc"
Returns: 481
"flcmhuzedpkatjwoigbxsvqnyr"
"ktvnnhiaasmqxfddocosnoshwn"
Returns: 213
"tfvpkgomeuyriqhjzwnaxscbdl"
"hkzdbfsouxxhkmxdfydgwqqyalosqxvykafgldxxgogejayqln"
Returns: 442
"kdliovyxcuajrnpsmthbfqgezw"
"vlkbyzrjbuwslnoshdmxoumbngatishhqthviysyihxwjztnil"
Returns: 421
"mxcqsfkpbawgzlrtdynheojiuv"
"ffpaceimfkarzthgjtevemdenvkuyvmkexzwcktluustvkersq"
Returns: 422
"xabuvlzychokmnpsdirqewftgj"
"imwrqmzipxtlvvmeqjovhwpeslpjbtwyoxrurxaahspdukeaei"
Returns: 438
"mwnruqchglsofaybitdvxejzkp"
"wwwfoirbrycfkvpstvtswsctykkqjotylrwrusfrpwxtulpuqz"
Returns: 412
"uwjseliorkqdtngfbamyvhzpxc"
"jnhtcg"
Returns: 52
"qdawxopznfimuvlsehrjytckbg"
"itgfggtzjopoqzyxfeamkyeqhiqmxbxpcrhxgcptcytnnxqcou"
Returns: 458
"isyrwvdnlbqjzcthumaxgpkfeo"
"ukjokxadbbzhaoqxopvlhrsztgqeytezrg"
Returns: 268
"xeplifckyhaodrmqzbuvwsjngt"
"yvvwzxkypuabekenotjttubcfjofciunddidotsaegnpzziyto"
Returns: 407
"pfxrlngakdjzcheyobmwsuivtq"
"rcdgbgkwet"
Returns: 65
"uswzhvxckleagipbydtjornfmq"
"wzmyntnsjynqpzehrccgljxtegwcqtkoctsp"
Returns: 330
"jgkmapryeobtxihnvcfulsdwqz"
"ybizsclklgaommexhetojxjbcnsdzpxcqwapzaigcpbfdqxrjb"
Returns: 385
"anbqfskrldzgjotyvwemxhcipu"
"vmpjscntltpaifjdgvlfqxfxaduhqrmhzbekymhpevrpgjaqhp"
Returns: 480
"gnyrlobmixsvcutejhdpkfzawq"
"wvasjmytqikqkmpqbshqutbbhjlgvuiqeskxhmpqivfzczxobi"
Returns: 416
"tdxayljmpungcrzhvqsoiebwfk"
"jlwqqpndnbbdggonfocrvxbmlnycjduvuxwlyenlntquaadbwu"
Returns: 411
"jzkyoavrswfnqdlpbxeucimtgh"
"oqnfywrsuvqwumjmaffgvynpevkzmog"
Returns: 264
"poeayickhxzfbdqlnrmstjwuvg"
"hmwjcvzwwujhzzingowbooukvvgpicfeafuixolqipyinruyui"
Returns: 443
"eucbakgtsrpxhqvflwomniydjz"
"vagcvydmdjnzkwzziasqporzqemqwdsvvzgrxtsnfktzwic"
Returns: 360
"wocnthkgupysamdjxvqizbferl"
"glvovglzznskvwzxarqrpqhodoaciqbcpafdwmlsnvhekkmcot"
Returns: 501
"qwertyuiopasdfghjklzxcvbnm"
"qmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqm"
Returns: 1225
"ab"
"b"
Returns: 0
"abcdefg"
"cb"
Returns: 1
"abcdefghijklmnopqrstuvwx"
"abcdefghijklmnopqrstuvwx"
Returns: 23