-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalphanumeric.java
62 lines (53 loc) · 1.77 KB
/
alphanumeric.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
56
57
58
59
60
61
// LeetCode_1805
// 2021.04.21
// Easy
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class alphanumeric {
public static void main(String[] args) {
// System.out.println(new alphanumeric().numDifferentIntegers("a123bc34d8ef34"));
System.out.println(new alphanumeric().numDifferentIntegers("167278959591294"));
}
public int numDifferentIntegers(String word) {
StringBuilder s = new StringBuilder(word);
List<Integer> list = new ArrayList<>();
List<String> sublist = new ArrayList<>();
HashSet<List> set1,set2;
String tmp ="";
int len = s.length();
for(int i = 0; i<s.length(); i++){
if(i == len-1)
s.append("e");
if(!(s.charAt(i) >= 'a' && s.charAt(i) <= 'z'))
tmp += s.charAt(i);
else
if(!tmp.equals("")) {
// if(word.substring(0,i-tmp.length()).contains(tmp))
// continue;
try {
list.add(Integer.valueOf(tmp));
}catch (NumberFormatException e){
sublist.add(tmp);
}
tmp ="";
}
}
set1 = new HashSet(list);
set2 = new HashSet(sublist);
return set1.size()+ set2.size();
}
}
/* SimpleSolution_출처: leetcode
class Solution {
public int numDifferentIntegers(String word) {
String[] arr = word.replaceAll("[a-zA-Z]", " ").split("\\s+");
Set<String> set = new HashSet<String>();
for (String str : arr) {
if (!str.isEmpty())
set.add(String.valueOf(str.replaceAll("^0*","")));
}
return set.size();
}
}
*/