Repository navigation
Expand file tree
/
Copy pathTest3.java
More file actions
75 lines (54 loc) · 1.83 KB
/
Copy pathTest3.java
File metadata and controls
75 lines (54 loc) · 1.83 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
package com.rbhatt.PracTest;
import java.sql.Timestamp;
import java.util.*;
/**
* Created by rbhatt22 on 7/29/20.
*/
public class Test3 {
public static int count = 0;
int countUniqueOpt(String[] arr) {
Set<String> resultList = new HashSet<>();
backtracking(arr, 3, resultList, new ArrayList<>());
return resultList.size();
}
void backtracking(String[] arr, int k, Set<String> resultList, List<Integer> indexList) {
if (indexList.size() == k) {
String tempString = arr[indexList.get(0)] + arr[indexList.get(1)] + arr[indexList.get(2)];
resultList.add(tempString);
} else {
for (int i = 0; i < arr.length; i++) {
if (!indexList.contains(i)) {
indexList.add(i);
backtracking(arr, k, resultList, indexList);
indexList.remove(indexList.size() - 1);
}
}
}
}
int countUnique(String [] arr)
{
Set<String> s = new TreeSet<>();
for (int i = 0; i<arr.length; i++)
{
for (int j = 0; j<arr.length; j++)
{
for (int k = 0; k<arr.length; k++)
{
count++;
if (i!=j && j != k && i!=k)
s.add((arr[i] +""+arr[j]+""+arr[k]));
}
}
}
return s.size();
}
public static void main(String[] args) {
Test3 ob = new Test3();
String[] arr = new String[] {"1", "1", "1", "2"};
Long startTime = System.currentTimeMillis();
System.out.println(ob.countUniqueOpt(arr));
Long endTime = System.currentTimeMillis();
System.out.println("Time taken: " + (endTime - startTime));
System.out.println("count: " + count);
}
}