Statistics

Problem Statement for "ValueOfString"

Problem Statement

You are given a String s consisting of lower case letters. We assign the letters 'a' to 'z' values of 1 to 26, respectively. We will denote the value assigned to the letter X by val[X]. For example, val['a'] = 1 and val['e'] = 5.

We define the value of the string s as follows. For each letter s[i], let k[i] be the number of letters in s that are less than or equal to s[i], including s[i] itself. Then, the value of s is defined to be the sum of k[i] * val[s[i]] for all valid i.

Given the string, compute and return the value of the string.

Definition

Class:
ValueOfString
Method:
findValue
Parameters:
String
Returns:
int
Method signature:
int findValue(String s)
(be sure your method is public)

Constraints

  • s will contain between 1 and 50 characters, inclusive.
  • s will consist of lowercase letters ('a'-'z').

Examples

  1. "babca"

    Returns: 35

    The value of this string is 2*4 + 1*2 + 2*4 + 3*5 + 1*2 = 35. We can get the value as follows. The first character is a 'b' which has value 2, and has 4 characters that are less than or equal to it in the string (i.e. the first, second, third and fifth character of the string). Thus, this first character contributes 2*4 to the sum. We can derive a similar expression for each of the other characters.

  2. "zz"

    Returns: 104

  3. "y"

    Returns: 25

  4. "aaabbc"

    Returns: 47

  5. "topcoder"

    Returns: 558

  6. "thequickbrownfoxjumpsoverthelazydog"

    Returns: 11187

  7. "zyxwvutsrqponmlkjihgfedcba"

    Returns: 6201

  8. "abcdefghijklmnopqrstuvwxyz"

    Returns: 6201

  9. "charlie"

    Returns: 297

  10. "valueofstring"

    Returns: 1502

  11. "a"

    Returns: 1

  12. "b"

    Returns: 2

  13. "z"

    Returns: 26

  14. "itwasthebestoftimesitwastheworstoftimesitwastheage"

    Returns: 23206

  15. "ofwisdomitwastheageoffoolishnessitwastheepochofbel"

    Returns: 20393

  16. "iefitwastheepochofincredulityitwastheseasonoflight"

    Returns: 20660

  17. "itwastheseasonofdarknessitwasthespringofhopeitwast"

    Returns: 22276

  18. "hewinterofdespairwehadeverythingbeforeuswehadnothi"

    Returns: 20124

  19. "ngbeforeuswewereallgoingdirecttoheavenwewereallgoi"

    Returns: 19714

  20. "ngdirecttheotherwayinshorttheperiodwassofarlikethe"

    Returns: 21097

  21. "presentperiodthatsomeofitsnoisiestauthoritiesinsis"

    Returns: 22556

  22. "tedonitsbeingreceivedforgoodorforevilinthesuperlat"

    Returns: 20182

  23. "ivedegreeofcomparisononlytherewereakingwithalargej"

    Returns: 19402

  24. "awandaqueenwithaplainfaceonthethroneofenglandthere"

    Returns: 18478

  25. "wereakingwithalargejawandaqueenwithafairfaceonthet"

    Returns: 18599

  26. "hroneoffranceinbothcountriesitwasclearerthancrysta"

    Returns: 20826

  27. "ltothelordsofthestatepreservesofloavesandfishestha"

    Returns: 21337

  28. "tthingsingeneralweresettledforeveritwastheyearofou"

    Returns: 21648

  29. "rlordonethousandsevenhundredandseventyfivespiritua"

    Returns: 21762

  30. "lrevelationswereconcededtoenglandatthatfavouredper"

    Returns: 19759

  31. "iodasatthismrssouthcotthadrecentlyattainedherfivea"

    Returns: 20915

  32. "ndtwentiethblessedbirthdayofwhomapropheticprivatei"

    Returns: 20691

  33. "nthelifeguardshadheraldedthesublimeappearancebyann"

    Returns: 17219

  34. "ouncingthatarrangementsweremadefortheswallowingupo"

    Returns: 20876

  35. "flondonandwestminstereventhecocklaneghosthadbeenla"

    Returns: 18832

  36. "idonlyarounddozenofyearsafterrappingoutitsmessages"

    Returns: 21857

  37. "asthespiritsofthisveryyearlastpastsupernaturallyde"

    Returns: 23665

  38. "ficientinoriginalityrappedouttheirsmeremessagesint"

    Returns: 20608

  39. "heearthlyorderofeventshadlatelycometotheenglishcro"

    Returns: 20200

  40. "wnandpeoplefromacongressofbritishsubjectsinamerica"

    Returns: 19529

  41. "whichstrangetorelatehaveprovedmoreimportanttothehu"

    Returns: 21637

  42. "manracethananycommunicationsyetreceivedthroughanyo"

    Returns: 20784

  43. "fthechickensofthecocklanebroodfrancelessfavouredon"

    Returns: 18065

  44. "thewholeastomattersspiritualthanhersisteroftheshie"

    Returns: 22046

  45. "ldandtridentrolledwithexceedingsmoothnessdownhillm"

    Returns: 20087

  46. "akingpapermoneyandspendingitundertheguidanceofherc"

    Returns: 18436

  47. "hristianpastorssheentertainedherselfbesideswithsuc"

    Returns: 21060

  48. "hhumaneachievementsassentencingayouthtohavehishand"

    Returns: 19772

  49. "scutoffhistonguetornoutwithpincersandhisbodyburned"

    Returns: 22042

  50. "alivebecausehehadnotkneeleddownintheraintodohonour"

    Returns: 19050

  51. "toadirtyprocessionofmonkswhichpassedwithinhisviewa"

    Returns: 21791

  52. "tadistanceofsomefiftyorsixtyyardsitislikelyenought"

    Returns: 22741

  53. "hatrootedinthewoodsoffranceandnorwaythereweregrowi"

    Returns: 21439

  54. "ngtreeswhenthatsuffererwasputtodeathalreadymarkedb"

    Returns: 20890

  55. "ythewoodmanfatetocomedownandbesawnintoboardstomake"

    Returns: 20584

  56. "acertainmovableframeworkwithasackandaknifeinitterr"

    Returns: 18995

  57. "ibleinhistoryitislikelyenoughthatintheroughouthous"

    Returns: 22263

  58. "esofsometillersoftheheavylandsadjacenttoparisthere"

    Returns: 20162

  59. "wereshelteredfromtheweatherthatverydayrudecartsbes"

    Returns: 21782

  60. "patteredwithrusticmiresnuffedaboutbypigsandroosted"

    Returns: 21514

  61. "inbypoultrywhichthefarmerdeathhadalreadysetapartto"

    Returns: 20901

  62. "behistumbrilsoftherevolutionbutthatwoodmanandthatf"

    Returns: 21592

  63. "armerthoughtheyworkunceasinglyworksilentlyandnoone"

    Returns: 22258

  64. "heardthemastheywentaboutwithmuffledtreadtheratherf"

    Returns: 20679

  65. "orasmuchastoentertainanysuspicionthattheywereawake"

    Returns: 22095

  66. "abcabc"

    Returns: 56

  67. "mmm"

    Returns: 117

  68. "mamzmmwijfqpoaiponqoerjq"

    Returns: 5033

  69. "jfjjfjjjjfjjfjfjjfjjjfjfj"

    Returns: 4634

  70. "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"

    Returns: 57500

  71. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

    Returns: 1225

  72. "abc"

    Returns: 14

  73. "bbbcccdddaaazzz"

    Returns: 1440

  74. "acdgknz"

    Returns: 368


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: