Repository navigation
Expand file tree
/
Copy pathMaxofNumberString.java
More file actions
79 lines (57 loc) · 2.26 KB
/
Copy pathMaxofNumberString.java
File metadata and controls
79 lines (57 loc) · 2.26 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.abn.dsalgos.problems;
import java.util.PriorityQueue;
public class MaxofNumberString {
public String maxNumber(String s1, String s2) {
if(s1.equals(s2)) {
return s1;
}
PriorityQueue<Integer> maxHeap1 = new PriorityQueue<>((n1, n2) -> n2 - n1);
PriorityQueue<Integer> maxHeap2 = new PriorityQueue<>((n1, n2) -> n2 - n1);
for(char c1 : s1.toCharArray()) {
maxHeap1.add(Character.getNumericValue(c1));
}
for(char c2 : s2.toCharArray()) {
maxHeap2.add(Character.getNumericValue(c2));
}
if(maxHeap1.size() > maxHeap2.size()) {
return getMaxNumber(maxHeap1);
} else if(maxHeap2.size() > maxHeap1.size()) {
return getMaxNumber(maxHeap2);
}
if(maxHeap1.peek() > maxHeap2.peek()) {
return getMaxNumber(maxHeap1);
} else if(maxHeap1.peek() < maxHeap2.peek()) {
return getMaxNumber(maxHeap2);
} else {
return getMaxFromHeaps(maxHeap1, maxHeap2);
}
}
private String getMaxNumber(PriorityQueue<Integer> maxHeap) {
StringBuilder sb = new StringBuilder();
while(!maxHeap.isEmpty()) {
sb.append(maxHeap.poll());
}
return sb.toString();
}
private String getMaxFromHeaps(PriorityQueue<Integer> maxHeap1, PriorityQueue<Integer> maxHeap2) {
StringBuilder sb = new StringBuilder();
while(!maxHeap1.isEmpty() && !maxHeap2.isEmpty()) {
if(maxHeap1.peek() == maxHeap2.peek()) {
sb.append(maxHeap1.poll());
maxHeap2.poll();
} else if (maxHeap1.peek() > maxHeap1.peek()){
return sb + getMaxNumber(maxHeap1);
} else {
return sb + getMaxNumber(maxHeap2);
}
}
return sb.toString();
}
public static void main(String[] args) {
MaxofNumberString maxofNumberString = new MaxofNumberString();
System.out.println(maxofNumberString.maxNumber("6328", "8563"));
System.out.println(maxofNumberString.maxNumber("5550","5505"));
System.out.println(maxofNumberString.maxNumber("11103", "1234"));
System.out.println(maxofNumberString.maxNumber("555","555"));
}
}