Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b78e72e

Browse files
newProblems
1 parent 6d42608 commit b78e72e

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

‎src/recursion/LongestCommonPrefix.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package recursion;
2+
3+
public class LongestCommonPrefix {
4+
5+
public static String helper(String common, String check, String prefix, int i){
6+
7+
if(i>=common.length() || i>=check.length()){
8+
9+
return prefix;
10+
11+
}
12+
if(common.charAt(i)!=check.charAt(i)){
13+
14+
return prefix;
15+
16+
}
17+
String ans=helper(common,check,prefix+common.charAt(i),i+1);
18+
return ans;
19+
20+
}
21+
22+
public static void answer(String input[]) {
23+
24+
String curr=input[0];
25+
26+
for(int i=1;i<input.length;i++){
27+
28+
curr=helper(curr,input[i],"",0);
29+
30+
31+
}
32+
33+
System.out.print(curr);
34+
35+
}
36+
}

‎src/stacks/CountAllPermuteGrthnN.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,38 @@ public static int permutations(int n) {
4444

4545

4646
}
47+
48+
49+
public static boolean checkSmallest(int n){
50+
51+
int prev=9;
52+
while(n>0){
53+
54+
int curr=n%10;
55+
if(prev<curr) {
56+
return false;
57+
}
58+
59+
prev=curr;
60+
n/=10;
61+
62+
}
63+
64+
return true;
65+
}
66+
67+
68+
69+
70+
public static int permutations2(int n) {
71+
72+
int count=0;
73+
for(int i=1;i<=n;i++){
74+
if(checkSmallest(i)){
75+
count++;
76+
}
77+
}
78+
79+
return count;
80+
}
4781
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /