Skip to content

Word Values Challenge in Java

The challenge

Given a string "abc" and assuming that each letter in the string has a value equal to its position in the alphabet, our string will have a value of 1 + 2 + 3 = 6. This means that: a = 1, b = 2, c = 3 ....z = 26.

You will be given a list of strings and your task will be to return the values of the strings as explained above multiplied by the position of that string in the list. For our purpose, position begins with 1.

nameValue ["abc","abc abc"] should return [6,24] because of [ 6 * 1, 12 * 2 ]. Note how spaces are ignored.

"abc" has a value of 6, while "abc abc" has a value of 12. Now, the value at position 1 is multiplied by 1 while the value at position 2 is multiplied by 2.

Input will only contain lowercase characters and spaces.

The solution in Java code

Option 1:

class Solution{ public static int [] nameValue(String [] arr){ String alpha = "abcdefghijklmnopqrstuvwxyz"; int[] out = new int[arr.length]; for (int i=; i<arr.length; i++) { int count = ; for (int j=; j<arr[i].length(); j++) { int val = alpha.indexOf(arr[i].charAt(j)); if (val>-1) count+=(val+1); } out[i] = count*(i+1); } return out; } }
Code language: Java (java)

Option 2:

class Solution{ public static int [] nameValue(String [] arr){ int[] result = new int[arr.length]; for (int i = ; i < arr.length; i++){ result[i] = arr[i].chars().filter(e -> e != ' ').map(e -> e - 96).sum() * (i+1); } return result; } }
Code language: Java (java)

Option 3:

import static java.util.stream.IntStream.rangeClosed; interface Solution { static int[] nameValue(String[] arr) { return rangeClosed(1, arr.length) .map(i -> i * arr[i - 1].chars().reduce(, (s, c) -> s + Math.max(c - 96, ))) .toArray(); } }
Code language: Java (java)

Test cases to validate our solution

import org.junit.Test; import static org.junit.Assert.assertArrayEquals; import org.junit.runners.JUnit4; import java.util.*; public class SolutionTest{ private static Random random = new Random(); private static int [] ba56(String [] arr){ int temp = ; int [] res = new int[arr.length]; for(int i = ;i<arr.length;++i){ for(char ch : arr[i].toCharArray()) if(Character. isLowerCase(ch)) temp += (int)ch - 96; res[i] = temp*(i+1); temp = ; } return res; } private static int random(int l, int u){ return random.nextInt(u-l)+l; } @Test public void basicTests(){ assertArrayEquals(new int[]{6,24},Solution.nameValue(new String[]{"abc","abc abc"})); assertArrayEquals(new int[]{351,282,330},Solution.nameValue(new String[]{"abcdefghijklmnopqrstuvwxyz","stamford bridge","haskellers"})); } @Test public void randomTests(){ String abc = " abcdefghijklmnopqrstuvwxyzabc"; for(int k=;k<100;k++){ int arrLen = random(1,10), i = ; String [] arr = new String[arrLen]; while (i < arrLen){ String st = ""; int len = random(5,15); for (int j = ; j < len; ++j) st += abc.charAt(random(,27)); arr[i] = st; i++; } assertArrayEquals(ba56(arr),Solution.nameValue(arr)); } } }
Code language: Java (java)

See also  Shortest Knight Path in Java
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x