Sam and substrings Discussions | Algorithms | HackerRank
We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
privatefinalstaticBigIntegerMOD=BigInteger.valueOf(1000000007);/* * Input: 123456 * Role of '2': 2, 20, 200, 2000, 20000, 200000 */publicstaticintsubstrings(Stringnumber){intlength=number.length();BigIntegertotalSum=BigInteger.valueOf(0);BigIntegermultiplier=BigInteger.valueOf(1);BigIntegersum=BigInteger.valueOf(0);// Iterate over each digit from the end to the startfor(inti=length-1;i>=0;i--){BigIntegerdigit=BigInteger.valueOf(Character.getNumericValue(number.charAt(i)));sum=(sum.add(digit.multiply(multiplier))).mod(MOD);totalSum=(totalSum.add(sum)).mod(MOD);multiplier=(multiplier.multiply(BigInteger.valueOf(10)).add(BigInteger.valueOf(1))).mod(MOD);}returntotalSum.intValue();}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sam and substrings
You are viewing a single comment's thread. Return to all comments →
JAVA SOLUTION