Friday, 31 January 2020

Context Free Grammar

A Context Free Grammar(CFG) is defined as G=(V,T,P,S) where
V=Finite set of Variables
T=Finite set of Terminals
P=Production rule which can be defined as 𝝰→𝛃 where 𝝰𝟄V and 𝞫→(V U T)*
S is a starting production

Example:

S→aSb|𝟄 is a grammar which accepts the language contains equal number of a's and equal number of b's

similarly

S→aSa|bSb|𝟄 is grammar which generates even length plaindrome

S→aSa|bSb is grammar which generates odd length palindrome

Saturday, 25 January 2020

/*Program to check whether two strings are Anagram or Not*/

Anagram Definition:

An anagram of a string is another string that contains same characters, only the order of characters can be different

Example: Weak, Wake
                 Mad, Dam

/* Program using Java*/

class StringAnagram
{
public static void main(String[] a) throws Exception
{
boolean flag=true;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("enter two strings");
str1=br.readLine();
str2=br.readLine();  
 int n1 = str1.length;
 int n2 = str2.length;
        if (n1 != n2)
            return false;
        char[] ch1 = new char[str1.length()];
        for (int i = 0; i < str1.length(); i++) {
            ch1[i] = str1.charAt(i);
        char[] ch2 = new char[str2.length()];
        for (int i = 0; i < str1.length(); i++) {
            ch2[i] = str1.charAt(i);
      
        Arrays.sort(str1);
        Arrays.sort(str2);
    
        for (int i = 0; i < n1; i++)
            if (str1[i] != str2[i])
               flag=false;
        if(flag==true)
                System.out.println("Strings are Anagram");
       else
                System.out.println("Strings are not Anagram");
 }
}