Showing posts with label pattern. Show all posts
Showing posts with label pattern. Show all posts

Sunday, 25 November 2012

write a function to check given string matches with given pattern





public class isMatchingWithWildcard {
public static void main(String [] args){
String pattern = "*abc*def*.doc*";
String str = "adsfabcxyzdefgh.do1docx";
if(isMatching(str, pattern))
System.out.print("Matching");
else
System.out.print("Not Matching");
}
public static Boolean isMatching(String str, String pattern){
int l = pattern.length();
if(pattern.lastIndexOf("*") == l-1)
pattern= (String) pattern.subSequence(0, l-1);
if(pattern.charAt(0) == '*')
pattern= (String) pattern.subSequence(1, pattern.length());
pattern = pattern.replace("*", "__");
String [] patternArray = pattern.split("__");
String sorttenString =str;
for(String aPattern:patternArray){
String [] temp = sorttenString.split(aPattern);
if(temp.length==1 && (aPattern == patternArray[patternArray.length-1] 
&& !sorttenString.toLowerCase().contains(aPattern.toLowerCase())))
return false;
str.substring(temp[0].length()+aPattern.length());
}
return true;
}
}

Let me know your thoughts

Saturday, 7 July 2012

Find the seed of a number.


Eg : 1716 = 143*1*4*3 =1716so 143 is the seed of 1716.
find all possible seed for a given number.

// Input   ||  OUTPUT 
//  1250        125       
//  174720      1456   

#include<stdio.h>
int main()
{
    int n,i,j,m,f=0;
    scanf("%d",&n);
    printf("\n");
    for(i=0;i<n;i++)
    {
        m=i;
        j=i;
        while(j)
        {
            m=m*(j%10);
            j=j/10;
        }
        if(m==n)
        {
            printf("\n%d",i);
            f=1;
        }
    }
    if(!f)
        printf("\nNo such number exit\n\n");
return 0;
}