ANAGRAM WA

This is a practice problem for an upcoming contest…i cant figure out a case where my code fails to produce the correct output…can ne1 pls point out the mistake…thanks in advance…:slight_smile:

ANAGRAM

Given an English text print out the list of words and their anagrams in their order of occurrence. Word A is an anagram of word B if it can be derived from word B by merely shuffling the letters in word B.

Important Points:

Text can contain words in upper case as well as lower case and punctuation marks

Anagrams are not to be case sensitive

The word and its anagrams are to be printed in the order of their occurrence in the text, separated by a blank.

If a word has no anagram in the text, then do not print it

If a word or its anagram occurs more than once do not print it again

Numbers are to be considered as valid words

Input:

A text containing K English words (where K <= 5000), with spaces and punctuation marks

Output:

The output should contain the word (in its order of occurrence in the text) and its set of anagrams in the text (again in their order of occurrence), separated by blanks

Each new list of words and anagrams should begin on a new line

All words in the output should be printed in lower case characters

TEST CASE:-

Parts of the world have sunlight for close to 24 hours during summer. Dan went to the north pole to lead an expedition during summer. He had a strap on his head to identify himself as the leader. Dan had to deal with the sun never going down for 42 consecutive days and his leadership strap soon became a blindfold. He wondered what kind of traps lay ahead of him.

OUTPUT:-

parts strap traps
24 42
dan and
lead deal

CODE…LINK!!!

ok yes almost there is WA did u saw that hint :: English Case:–dp,pd ap,pa dssd and after that if u get good Job do tell me if it does not has presentation error as i get presentation error with good job.

dude…in ENGLISH after every punctuation mark there is a space…as far as i know!!!

What about apostrophes?

that is an ambiguity…it has not been mentioned properly in the problem statement…but i dont think we have to print any punctuations…so i have removed that too…also when i was solving the ques i saw the stats…i saw arnd 200+ ppl trying it and none had got AC!!!

maybe the test cases are faulty…not sure!!!

dude…in ENGLISH after every punc there is a space…!!!

no output…but the case does not follow english rules!!!

see this link…http://www.grammarbook.com/punctuation/spacing.asp
or maybe go to ur primary section teacher…:stuck_out_tongue:

dude…we are speaking of ENGLISH, and in proper ENGLISH, always punctuations like “,”, “.”, “?”, “!” etc. have a space after it…and what do u mean by puncs in my program…im not writing ne english essay out there…:stuck_out_tongue:

@kunal361 u r rude:) and eg for u : Kunal?uknal how many words here?? work on it it will give u correct answer just do it rather than telling me English lessons :stuck_out_tongue:

sorry if i was rude…:slight_smile:
but still im not understanding y r u telling me to try this case…also if i m unable to understand what you may want to convey…pls do explain it…:slight_smile:

simple as that split using ? , . etc also

import java.io.;
import java.util.
;
class Anagram
{
String str,a[],result[];
StringTokenizer st;
StringBuffer presenttoken,bz;
int nooftoken,i,lenpresenttoken,j,ct,len,matches,m,n;
char ch,chara,charb;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
void getInput()throws IOException
{
System.out.println(“Enter a String ?? “);
str=in.readLine();
str=str.toLowerCase();
//System.out.println(”\nString agter conversion to lowecase is :: “+str+”\n”);
convertToTokens();
removePunctuation();
calculate();
}
void convertToTokens()
{
st=new StringTokenizer(str);
nooftoken=st.countTokens();
a=new String[nooftoken];
result=new String[nooftoken];
for(i=0; i<nooftoken; i++)
{
a[i]=st.nextToken().toString();
}
/System.out.println("\nIntial array :: ");
display();
/
}
void removePunctuation()
{
for(i=0; i<nooftoken; i++)
{
presenttoken=new StringBuffer(a[i]);
lenpresenttoken=presenttoken.length();
ch=presenttoken.charAt(lenpresenttoken-1);
if( (ch==’.’) || (ch==’?’) || (ch==’!’) )
{
presenttoken=presenttoken.deleteCharAt(lenpresenttoken-1);
a[i]=presenttoken.toString();
}
}
/System.out.println("\nArray after removing punctuation :: \n");
display();
/
}
void calculate()
{
norepeats();
findAnagram();
}
void norepeats()
{
for(i=0; i<(nooftoken-1); i++)
{
for(j=i+1; j<nooftoken; j++)
{
if(a[i].equals(a[j]))
a[j]="";
}
}
/System.out.println("\nString after no repeats :: \n");
display();
/
}
void display()
{
for(i=0; i<nooftoken; i++)
{
System.out.print(a[i]+" “);
}
}
void findAnagram()
{
for(i=0; i<(nooftoken-1); i++)
{
if(a[i].length()==0)
continue;
else
{
result[0]=a[i];
ct=1;
for(j=i+1; j<nooftoken; j++)
{
if(a[j].length()==0)
continue;
else
{
if(compareLengths(a[i],a[j]))
{
if(areAnagram(a[i],a[j]))
{
addtoResult(a[j],ct);
a[j]=”";
ct++;
}
else
{
continue;
}
}
else
{
continue;
}
}
}
if(ct!=1)
{
//System.out.println("Result :: “);
displayResult();
System.out.println();
}
}
}
}
boolean compareLengths(String a, String b)
{
if(a.length()==b.length())
return true;
else
return false;
}
boolean areAnagram(String a,String b)
{
bz=convertToStringBuffer(b);
len=a.length();
matches=0;
for(m=0; m<len; m++)
{
chara=a.charAt(m);
for(n=0; n<len; n++)
{
charb=bz.charAt(n);
if(chara==charb)
{
matches=matches+1;
bz.deleteCharAt(n);
bz.insert(n,’ ');
break;
}
else
{
continue;
}
}
}
if(matches==len)
return true;
else
return false;
}
StringBuffer convertToStringBuffer(String str)
{
return(new StringBuffer(str));
}
void addtoResult(String a,int b)
{
result[b]=a;
}
void displayResult()
{
for(int u=0; u<ct; u++)
{
System.out.print(result[u]+” ");
}
}
}
class NewAnagram
{
public static void main(String args[])throws IOException
{
Anagram ob=new Anagram();
ob.getInput();
}
}

contest over so this was mine :slight_smile:

 #include <sstream>
 #include <iostream>
 #include<cstdio>
 #include<cstdlib>
 #include <cstring>
 #include <string>
 #include<algorithm>
 using namespace std;
 int main ()
 {
 std::string str;
 string str1,str2,str3;
 std::getline(cin, str);
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
str1=str;
string arr[5010],arr1[5010];
int l=0,i,l1=0,j,flag=0,mp=0,mp1=0;

 char * cstr = new char [str.length()+1];
 std::strcpy (cstr, str.c_str());
 // cstr now contains a c-string copy of str

 char * p = std::strtok (cstr,".,;!? ");
 while (p!=0)
 {
//std::cout << p << '\n';
arr[l++]=p;
p = strtok(NULL,".,;!? ");
}
delete[] cstr;
 char * cstr1 = new char [str1.length()+1];
 std::strcpy (cstr1, str.c_str());

 // cstr now contains a c-string copy of str

 char * p1= std::strtok (cstr1,".,;!? ");
 while (p1!=0)
 {
//std::cout << p << '\n';
arr1[l1++]=p1;
p1 = strtok(NULL,".,;!? ");
}
  delete[] cstr1;
 //std::sort(arr, arr + l);
 for(i=0;i<l-1;i++)
 {
  for(j=i+1;j<l;j++)
  {
      if(arr1[j]!="0" && arr1[i]!="0" &&arr1[i]==arr1[j])
      arr1[j]="0";
  }
  }
   for(i=0;i<l;i++)
   {
  str2=arr1[i];
  std::sort(str2.begin(), str2.end());
  arr1[i]=str2;
    }
    /*for(i = 0; i <l; i++)
    {
    cout << arr[i]<<" "<<arr1[i]<< endl;
     }*/
  //cout<<l;
    for(i=0;i<l-1;i++)
   {
 str3="";
 if(mp1>0)
  str3.append("\n");
  flag=0;
   mp=0;
  if(arr1[i]!="0")
  {
     for(j=i+1;j<l;j++)
  {
     if(arr1[j]!="0" && (arr1[i]==arr1[j]) && flag==0)
    {
    str3.append(arr[i]);
    str3.append(" ");
    str3.append(arr[j]);
    str3.append(" ");
   // cout<<arr[i]<<" "<<arr[j]<<" ";
    arr1[j]="0";
    flag=1;
    mp=1;
   mp1++;
   }
   else if((arr1[i]==arr1[j]) && flag==1)
   {
    arr1[j]="0";
    //cout<<arr[j]<<" ";

    str3.append(arr[j]);
    str3.append(" ");

    mp=1;
   }
 }
 if(str3!=""&& str3!="\n"&&mp1>0)
 cout<<str3;
 }
 }
 //cout<<"\n";
 return 0;
  }

finally i alligned all :slight_smile: