You are viewing a single comment's thread. Return to all comments →
// Using java and all test case passed
import java.util.*;
public class Solution {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Key pair value for alphabets Map<String, Integer> map = new HashMap<>(); map.put("A", 1); map.put("H", 8); map.put("O", 15); map.put("V", 22); map.put("B", 2); map.put("I", 9); map.put("P", 16); map.put("W", 23); map.put("C", 3); map.put("J", 10); map.put("Q", 17); map.put("X", 24); map.put("D", 4); map.put("K", 11); map.put("R", 18); map.put("Y", 25); map.put("E", 5); map.put("L", 12); map.put("S", 19); map.put("Z", 26); map.put("F", 6); map.put("M", 13); map.put("T", 20); map.put("G", 7); map.put("N", 14); map.put("U", 21); // generate list and sort it correspondingly int n = sc.nextInt(); ArrayList <String> lst = new ArrayList<String>(); for(int i=0;i<n ; i++){ String str = sc.next(); lst.add(str); } Collections.sort(lst); // check for a string in list with its position and calculate the logic int q = sc.nextInt(); for(int i=0;i<q ; i++){ int sum = 0; String str = sc.next(); int pos = lst.indexOf(str); for(int j=0;j<str.length();j++){ String c = String.valueOf(str.charAt(j)); int num = map.get(c); sum += num ; } sum = sum*(pos+1); System.out.println(sum); } }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #22: Names scores
You are viewing a single comment's thread. Return to all comments →
// Using java and all test case passed
import java.util.*;
public class Solution {
}