-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffiRecursion.java
131 lines (107 loc) · 3.15 KB
/
DiffiRecursion.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
public class DiffiRecursion {
public static int first =-1;
public static int last =-1;
public static boolean [] map = new boolean[26];
// public static void towerOfHanoi(int n, String src,String Helper,String Dest)
// {// base case
// if(n==1)
// {
// System.out.println("transfer disk "+ n+" "+ "from"+" "+src +" "+"to"+" "+Dest);
// return;
// }
// towerOfHanoi(n-1, src, Dest, Helper);
// System.err.println("transfer disk "+ n+" "+ "from"+" "+src +" "+"to"+" "+Dest);
// towerOfHanoi(n-1, Helper, src, Dest);
// }
// public static void RevStr(String str,int inx)
// {//time complexity = O(n)
// if(inx==0)
// {
// System.out.println(str.charAt(inx));
// return;
// }
// System.out.println(str.charAt(inx));
// RevStr(str, inx-1);
// }
public static void FindOcurr(String str, int idx, char element)
{ if(idx==str.length())
{
System.out.println(first);
System.out.println(last);
return;
}
char Currchar = str.charAt(idx);
if(Currchar == element)
{
if(first ==-1)
{
first = idx;
}
else{
last= idx;
}
}
FindOcurr(str, idx+1, element);
}
// public static boolean SortedArray(int arr[],int idx)
// { //we are chcking aray is sorted or not
// //time complexity = O(n)
// if(idx==arr.length-1)
// {
// return true;
// }
// if(arr[idx] <arr[idx+1])
// return SortedArray(arr, idx+1);
// else
// return false;
// }
// public static void MovetoEnd(String str, int idx, char element,int count, String newstr)
// {//Time complexity = O(n)
// if(idx==str.length()){
// for(int i=0;i<count;i++)
// {
// newstr+= element;
// }
// System.out.println(newstr);
// return;
// }
// char Currchar = str.charAt(idx);
// if(Currchar==element)
// {
// count++;
// MovetoEnd(str, idx+1, element, count, newstr);
// }
// else{
// newstr += Currchar;
// MovetoEnd(str, idx+1, element, count, newstr);
// }
// }
public static void RemoveDup(String str, int idx, String newstr)
{//Time complexity = O(n)
if(idx==str.length()){
System.out.println(newstr);
return;
}
char Currchar = str.charAt(idx);
if(map[Currchar -'a'])
RemoveDup(str, idx+1, newstr);
else
{
newstr +=Currchar;
map[Currchar -'a']= true;
RemoveDup(str, idx+1, newstr);
}
}
public static void main(String[] args) {
// int n=3;
// towerOfHanoi(n, "S", "H", "D");
// String str ="abcd";
// RevStr(str, str.length()-1);
// String str = "habiaathcafaio";
// FindOcurr(str, 0, 'a');
// int arr[]= {1,5,10};
// System.out.println(SortedArray(arr, 0));
String str ="abcadd";
RemoveDup(str, 0, "");
}
}