-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1945-sum-of-digits-of-string-after-convert.java
55 lines (46 loc) · 1.37 KB
/
1945-sum-of-digits-of-string-after-convert.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public int getLucky(String s, int k) {
HashMap<Character, Integer > map = letters();
ArrayList<Integer> temp = new ArrayList<>();
for(int i = 0 ; i < s.length(); i++){
temp.add(map.get(s.charAt(i)));
}
// System.out.println(temp);
int ans1 = transform1(temp);
if(k>1){
int p = 1;
while(p < k){
int tempAns = transform2(ans1);
ans1 = tempAns;
p++;
}
return ans1;
}
return ans1;
}
private int transform1(List<Integer> temp){
int sum =0;
for(int i = 0 ; i < temp.size(); i++){
sum += transform2(temp.get(i));
// System.out.println("sum " + sum);
}
return sum;
}
private int transform2(int num){
int ans = 0;
while(num > 0){
int tempNum = num %10;
ans = ans + tempNum;
num = num /10;
}
return ans;
}
private HashMap<Character,Integer> letters(){
HashMap<Character, Integer > map = new HashMap<>();
int i =1;
for(char ch = 'a'; ch <= 'z'; ch++) {
map.put(ch,i++);
}
return map;
}
}