-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recursion_PracticeProblems.java
69 lines (56 loc) · 2.08 KB
/
Recursion_PracticeProblems.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
public class Recursion_PracticeProblems {
public static void all_occurences(int[] arr, int i, int key){
if (i == arr.length) return;
if (arr[i]==key) System.out.print(i + " ");
all_occurences(arr, i+1, key);
}
public static void number_to_words(int n){
if (n==0){
return;
}
String[] words = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int last_digit = n%10;
number_to_words(n/10);
System.out.print(words[last_digit] + " ");
}
public static int string_length(String str){
if (str.length() == 0) return 0;
return string_length(str.substring(1)) + 1;
}
public static int same_char_at_beg_end(String str, int i, int j, int n){ //O(3^n)
// Base cases
if (n==1) return 1;
if (n<=0) return 0;
// AUB = A + B + A intersection B
int res = same_char_at_beg_end(str, i+1, j, n-1) +
same_char_at_beg_end(str, i, j-1, n-1) -
same_char_at_beg_end(str, i+1, j-1, n-2);
if (str.charAt(i)==str.charAt(j)) res++;
return res;
}
public static int optimized_same_char(String str){ //0(n)
int n = str.length();
int ans = 0;
int[] words = new int[26];
for (int i=0; i<n; i++){
words[str.charAt(i)-'a']++;
}
for (int i=0; i<words.length; i++){
ans = ans + (words[i]*(words[i]+1))/2;
}
return ans;
}
public static void main(String[] args) {
int[] arr= {2, 4, 2, 5, 5, 2, 6, 6 ,3, 2};
all_occurences(arr, 0,2);
System.out.println();
number_to_words(1987);
System.out.println();
String name = "Parth";
System.out.println(string_length(name));
String sample = "abcab";
int res = same_char_at_beg_end(sample, 0, sample.length()-1, sample.length());
System.out.println(res);
System.out.println(optimized_same_char(sample));
}
}