Statistics

Problem Statement for "TypingDistance"

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 String keyboard and a String word, describing the keyboard and the word Jakub wants to write. Return the minimum distance he will have to move his finger in order to type the word on the keyboard.

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

  1. "qwertyuiop"

    "potter"

    Returns: 8

    The example from the problem statement.

  2. "tc"

    "tctcttccctccccttc"

    Returns: 9

  3. "a"

    "aaaaaaaaaaa"

    Returns: 0

  4. "kwadrutove"

    "rowerowe"

    Returns: 39

  5. "qwertyuiopasdfghjklzxcvbnm"

    "topcodersingleroundmatchgoodluckhavefun"

    Returns: 322

  6. "d"

    "dddddddddddddddddddddddddddddddddddddddddddddddddd"

    Returns: 0

  7. "co"

    "coccooccocoococoocccoo"

    Returns: 13

  8. "vej"

    "ejevvevevjjjejvevvvevvejeeeejjjejjveevjjvjejevveve"

    Returns: 40

  9. "lsit"

    "sltittsltiitststsitlltitltiilsillltiliitiss"

    Returns: 58

  10. "xkumo"

    "xmmxuuuxmxooxxumkxokuxokkxkmoxxkkumukkxkxkkmxmxoom"

    Returns: 81

  11. "tqlbdo"

    "oboq"

    Returns: 8

  12. "ecjskyi"

    "eesjjyykeysccsseyskkejeeyciikkcesejjeejysyycceyies"

    Returns: 101

  13. "awcyepqf"

    "ppfywwyceayfyaacaypfyecpcqyywfafqyp"

    Returns: 90

  14. "kdqtbmnep"

    "bpnkdqtdnq"

    Returns: 26

  15. "vhtgywqkem"

    "wvkkvwqqtqetevqqmhqmvmythemmvhtmtwvkhqvvgegekmhkwy"

    Returns: 209

  16. "tqrgnikvyxj"

    "gjtynnynyviitjyikgxrgjivyir"

    Returns: 103

  17. "cahvkibdmejt"

    "vhejijvabvimajedbhtackvabvmkitjceatchtjbvtijbeckvi"

    Returns: 226

  18. "kfsqvzpwbtmhg"

    "qqphwmzzkbpmvbhggbfzpkwwbbfvwggkhkzwwvfvvmhpsspwtf"

    Returns: 182

  19. "izkldmubhqonrg"

    "zzoimlbgrgdkhirdgdigbl"

    Returns: 128

  20. "qilwphgasdcfvun"

    "hcpflldugvslficifnvvwahcuqhiacpsgispvvvqdgwlavcwsp"

    Returns: 259

  21. "efsxwdybtivcpurk"

    "yidkxkbifcvbbtpkfyyyrxvdpurcfssbepibcykyxwrwpcb"

    Returns: 247

  22. "sipndlwvyzcxtemkg"

    "ntekzwvgxyewygenltgvmssvgmcnytcelcnvmdxexlnglypykw"

    Returns: 275

  23. "weiuosvgkhtbjdymrx"

    "drbdetjgitumrduhhdwdshohxeehtyewjrytwy"

    Returns: 239

  24. "uatspmolvizqnhryedg"

    "vqgaahssnnmeepmuqevhdhovooqeezeqszinpvahooazganumt"

    Returns: 296

  25. "phjafsgzoirxqevkcumy"

    "earxykijjrcjcfryygucfh"

    Returns: 152

  26. "zdpvagwsouileyjnfqrcm"

    "alqnueweyiqsnszzczagdoiuquivpugrvenszjccicvqzrlnaa"

    Returns: 352

  27. "damgreuvtjiqswkfxzopbc"

    "tticddvdizidsfqjgmicwwqojjisofsrpvwggmzrazsfofpffs"

    Returns: 294

  28. "pnmjsqldkwfhoyxrzibvtug"

    "mnkqjwbqbptdzinxzjrytrfuqvsvdqpskglxqbyrtixgqdkvfz"

    Returns: 420

  29. "tfqvbawsdeuypcgxmirlokjz"

    "zayapdleckjikgoflvwykzktkdpwsmrc"

    Returns: 254

  30. "ijzqafksuobcmlypgthrdwnex"

    "obyhorcajwymhsfotmhyqodtm"

    Returns: 157

  31. "ljkhuogfrqwycvabszxptiendm"

    "feiusvgkvwdocbciqktviclbsgvzdonuzmlktyybolrjxynilc"

    Returns: 481

  32. "flcmhuzedpkatjwoigbxsvqnyr"

    "ktvnnhiaasmqxfddocosnoshwn"

    Returns: 213

  33. "tfvpkgomeuyriqhjzwnaxscbdl"

    "hkzdbfsouxxhkmxdfydgwqqyalosqxvykafgldxxgogejayqln"

    Returns: 442

  34. "kdliovyxcuajrnpsmthbfqgezw"

    "vlkbyzrjbuwslnoshdmxoumbngatishhqthviysyihxwjztnil"

    Returns: 421

  35. "mxcqsfkpbawgzlrtdynheojiuv"

    "ffpaceimfkarzthgjtevemdenvkuyvmkexzwcktluustvkersq"

    Returns: 422

  36. "xabuvlzychokmnpsdirqewftgj"

    "imwrqmzipxtlvvmeqjovhwpeslpjbtwyoxrurxaahspdukeaei"

    Returns: 438

  37. "mwnruqchglsofaybitdvxejzkp"

    "wwwfoirbrycfkvpstvtswsctykkqjotylrwrusfrpwxtulpuqz"

    Returns: 412

  38. "uwjseliorkqdtngfbamyvhzpxc"

    "jnhtcg"

    Returns: 52

  39. "qdawxopznfimuvlsehrjytckbg"

    "itgfggtzjopoqzyxfeamkyeqhiqmxbxpcrhxgcptcytnnxqcou"

    Returns: 458

  40. "isyrwvdnlbqjzcthumaxgpkfeo"

    "ukjokxadbbzhaoqxopvlhrsztgqeytezrg"

    Returns: 268

  41. "xeplifckyhaodrmqzbuvwsjngt"

    "yvvwzxkypuabekenotjttubcfjofciunddidotsaegnpzziyto"

    Returns: 407

  42. "pfxrlngakdjzcheyobmwsuivtq"

    "rcdgbgkwet"

    Returns: 65

  43. "uswzhvxckleagipbydtjornfmq"

    "wzmyntnsjynqpzehrccgljxtegwcqtkoctsp"

    Returns: 330

  44. "jgkmapryeobtxihnvcfulsdwqz"

    "ybizsclklgaommexhetojxjbcnsdzpxcqwapzaigcpbfdqxrjb"

    Returns: 385

  45. "anbqfskrldzgjotyvwemxhcipu"

    "vmpjscntltpaifjdgvlfqxfxaduhqrmhzbekymhpevrpgjaqhp"

    Returns: 480

  46. "gnyrlobmixsvcutejhdpkfzawq"

    "wvasjmytqikqkmpqbshqutbbhjlgvuiqeskxhmpqivfzczxobi"

    Returns: 416

  47. "tdxayljmpungcrzhvqsoiebwfk"

    "jlwqqpndnbbdggonfocrvxbmlnycjduvuxwlyenlntquaadbwu"

    Returns: 411

  48. "jzkyoavrswfnqdlpbxeucimtgh"

    "oqnfywrsuvqwumjmaffgvynpevkzmog"

    Returns: 264

  49. "poeayickhxzfbdqlnrmstjwuvg"

    "hmwjcvzwwujhzzingowbooukvvgpicfeafuixolqipyinruyui"

    Returns: 443

  50. "eucbakgtsrpxhqvflwomniydjz"

    "vagcvydmdjnzkwzziasqporzqemqwdsvvzgrxtsnfktzwic"

    Returns: 360

  51. "wocnthkgupysamdjxvqizbferl"

    "glvovglzznskvwzxarqrpqhodoaciqbcpafdwmlsnvhekkmcot"

    Returns: 501

  52. "qwertyuiopasdfghjklzxcvbnm"

    "qmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqmqm"

    Returns: 1225

  53. "ab"

    "b"

    Returns: 0

  54. "abcdefg"

    "cb"

    Returns: 1

  55. "abcdefghijklmnopqrstuvwx"

    "abcdefghijklmnopqrstuvwx"

    Returns: 23


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: