This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class SimpleSpamDetectorInefficient { | |
| public static boolean doesDuplicatingMatch(String s,String p){ | |
| if(p.length()>s.length()) //if pattern is larger than the original string | |
| return false; | |
| else if(s.length()==0 && p.length()==0) //if both empty | |
| return true; | |
| else if(s.length()==0 || p.length()==0) //if one empty | |
| return false; | |
| else if(Character.toLowerCase(s.charAt(0)) == Character.toLowerCase(p.charAt(0))) | |
| return doesDuplicatingMatch(s.substring(1),p.substring(1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class SimpleSpamDetectorSlightlyInefficient { | |
| public static boolean doesDuplicatingMatch(String s,int s_i, int s_j, String p,int p_i, int p_j){ | |
| if(p_j-p_i>s_j-s_i) //if pattern is larger than the original string | |
| return false; | |
| else if(s_j-s_i==0 && p_j-p_i==0) //if both empty | |
| return true; | |
| else if(s_j<s_i||p_j<p_i) | |
| return false; | |
| else if(Character.toLowerCase(s.charAt(s_i)) == Character.toLowerCase(p.charAt(p_i))) | |
| return doesDuplicatingMatch(s,s_i+1,s_j,p,p_i+1,p_j) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class SimpleSpamDetector { | |
| public static boolean doesDuplicatingMatch(String s,int s_i, int s_j, String p,int p_i, int p_j){ | |
| if(p_j-p_i>s_j-s_i) //if pattern is larger than the original string | |
| return false; | |
| else if(s_j-s_i==0 && p_j-p_i==0) //if both empty | |
| return true; | |
| else if(s_j<s_i||p_j<p_i) | |
| return false; | |
| else if(Character.toLowerCase(s.charAt(s_i)) == Character.toLowerCase(p.charAt(p_i))) | |
| return doesDuplicatingMatch(s,s_i+1,s_j,p,p_i+1,p_j) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CompletingBrackets { | |
| public static String complete(String text){ | |
| StringBuilder result= new StringBuilder(); | |
| int count=0; | |
| for(int i=0;i<text.length();i++){ | |
| if(text.charAt(i)=='[') count++; | |
| else if(text.charAt(i)==']') count--; | |
| } | |
| if(text.charAt(0)==']'){ | |
| result.append('['); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class FewestFactors { | |
| public static int number(int[] digits){ | |
| LinkedList<Integer> adapter= new LinkedList<Integer>(); | |
| for(int i=digits.length-1;i>=0;i--) | |
| adapter.addFirst(digits[i]); | |
| HashSet<LinkedList<Integer>> combinitions=combinate(adapter); | |
| int min=Integer.MAX_VALUE; | |
| int min_num=0; | |
| for(LinkedList<Integer> c:combinitions){ | |
| int num=convertToInteger(c); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<math.h> | |
| #include<stdlib.h> | |
| int is_palindrome(int n){ | |
| int len=0; | |
| while(n/(int)pow(10,len)) len++; | |
| while(len>1){ | |
| int mul=pow(10,len-1); | |
| int d1=n/mul; | |
| int d2=n%10; | |
| if(d1!=d2) return 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdlib.h> | |
| #include<stdio.h> | |
| long int gcd(long int a, long int b){ | |
| if(b==0) return a; | |
| else return gcd(b,a%b); | |
| } | |
| long int lcm(long int a, long int b){ | |
| return a*b/gcd(a,b); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| #include<math.h> | |
| int is_prime(int p){ | |
| if(p<=1) return 0; | |
| int i=2; | |
| int sqrt_p=sqrt(p); | |
| while(i<=sqrt_p) | |
| if(p%i++==0) | |
| return 0; | |
| return 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.math.BigInteger; | |
| public class FactorialDigitSum { | |
| public FactorialDigitSum() { | |
| } | |
| public static int calculate(int n){ | |
| return digitSum(factorial(new BigInteger(""+n))).intValue(); | |
| } | |
| private static BigInteger digitSum(BigInteger n){ | |
| if(n.compareTo(BigInteger.TEN)==-1) return n; | |
| else return n.mod(BigInteger.TEN).add(digitSum(n.divide(BigInteger.TEN))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdlib.h> | |
| #include<stdio.h> | |
| int get_left_most_1(int n){ | |
| int pos=0; | |
| while(n>1){ | |
| n>>=1; | |
| pos++; | |
| } | |
| return pos; | |
| } |