Problem Statement
You are given a
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
"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.
"zz"
Returns: 104
"y"
Returns: 25
"aaabbc"
Returns: 47
"topcoder"
Returns: 558
"thequickbrownfoxjumpsoverthelazydog"
Returns: 11187
"zyxwvutsrqponmlkjihgfedcba"
Returns: 6201
"abcdefghijklmnopqrstuvwxyz"
Returns: 6201
"charlie"
Returns: 297
"valueofstring"
Returns: 1502
"a"
Returns: 1
"b"
Returns: 2
"z"
Returns: 26
"itwasthebestoftimesitwastheworstoftimesitwastheage"
Returns: 23206
"ofwisdomitwastheageoffoolishnessitwastheepochofbel"
Returns: 20393
"iefitwastheepochofincredulityitwastheseasonoflight"
Returns: 20660
"itwastheseasonofdarknessitwasthespringofhopeitwast"
Returns: 22276
"hewinterofdespairwehadeverythingbeforeuswehadnothi"
Returns: 20124
"ngbeforeuswewereallgoingdirecttoheavenwewereallgoi"
Returns: 19714
"ngdirecttheotherwayinshorttheperiodwassofarlikethe"
Returns: 21097
"presentperiodthatsomeofitsnoisiestauthoritiesinsis"
Returns: 22556
"tedonitsbeingreceivedforgoodorforevilinthesuperlat"
Returns: 20182
"ivedegreeofcomparisononlytherewereakingwithalargej"
Returns: 19402
"awandaqueenwithaplainfaceonthethroneofenglandthere"
Returns: 18478
"wereakingwithalargejawandaqueenwithafairfaceonthet"
Returns: 18599
"hroneoffranceinbothcountriesitwasclearerthancrysta"
Returns: 20826
"ltothelordsofthestatepreservesofloavesandfishestha"
Returns: 21337
"tthingsingeneralweresettledforeveritwastheyearofou"
Returns: 21648
"rlordonethousandsevenhundredandseventyfivespiritua"
Returns: 21762
"lrevelationswereconcededtoenglandatthatfavouredper"
Returns: 19759
"iodasatthismrssouthcotthadrecentlyattainedherfivea"
Returns: 20915
"ndtwentiethblessedbirthdayofwhomapropheticprivatei"
Returns: 20691
"nthelifeguardshadheraldedthesublimeappearancebyann"
Returns: 17219
"ouncingthatarrangementsweremadefortheswallowingupo"
Returns: 20876
"flondonandwestminstereventhecocklaneghosthadbeenla"
Returns: 18832
"idonlyarounddozenofyearsafterrappingoutitsmessages"
Returns: 21857
"asthespiritsofthisveryyearlastpastsupernaturallyde"
Returns: 23665
"ficientinoriginalityrappedouttheirsmeremessagesint"
Returns: 20608
"heearthlyorderofeventshadlatelycometotheenglishcro"
Returns: 20200
"wnandpeoplefromacongressofbritishsubjectsinamerica"
Returns: 19529
"whichstrangetorelatehaveprovedmoreimportanttothehu"
Returns: 21637
"manracethananycommunicationsyetreceivedthroughanyo"
Returns: 20784
"fthechickensofthecocklanebroodfrancelessfavouredon"
Returns: 18065
"thewholeastomattersspiritualthanhersisteroftheshie"
Returns: 22046
"ldandtridentrolledwithexceedingsmoothnessdownhillm"
Returns: 20087
"akingpapermoneyandspendingitundertheguidanceofherc"
Returns: 18436
"hristianpastorssheentertainedherselfbesideswithsuc"
Returns: 21060
"hhumaneachievementsassentencingayouthtohavehishand"
Returns: 19772
"scutoffhistonguetornoutwithpincersandhisbodyburned"
Returns: 22042
"alivebecausehehadnotkneeleddownintheraintodohonour"
Returns: 19050
"toadirtyprocessionofmonkswhichpassedwithinhisviewa"
Returns: 21791
"tadistanceofsomefiftyorsixtyyardsitislikelyenought"
Returns: 22741
"hatrootedinthewoodsoffranceandnorwaythereweregrowi"
Returns: 21439
"ngtreeswhenthatsuffererwasputtodeathalreadymarkedb"
Returns: 20890
"ythewoodmanfatetocomedownandbesawnintoboardstomake"
Returns: 20584
"acertainmovableframeworkwithasackandaknifeinitterr"
Returns: 18995
"ibleinhistoryitislikelyenoughthatintheroughouthous"
Returns: 22263
"esofsometillersoftheheavylandsadjacenttoparisthere"
Returns: 20162
"wereshelteredfromtheweatherthatverydayrudecartsbes"
Returns: 21782
"patteredwithrusticmiresnuffedaboutbypigsandroosted"
Returns: 21514
"inbypoultrywhichthefarmerdeathhadalreadysetapartto"
Returns: 20901
"behistumbrilsoftherevolutionbutthatwoodmanandthatf"
Returns: 21592
"armerthoughtheyworkunceasinglyworksilentlyandnoone"
Returns: 22258
"heardthemastheywentaboutwithmuffledtreadtheratherf"
Returns: 20679
"orasmuchastoentertainanysuspicionthattheywereawake"
Returns: 22095
"abcabc"
Returns: 56
"mmm"
Returns: 117
"mamzmmwijfqpoaiponqoerjq"
Returns: 5033
"jfjjfjjjjfjjfjfjjfjjjfjfj"
Returns: 4634
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"
Returns: 57500
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Returns: 1225
"abc"
Returns: 14
"bbbcccdddaaazzz"
Returns: 1440
"acdgknz"
Returns: 368