Re: [algogeeks] doubt in checking if 2 strings are anagrams
Things can go very bad in case the string length is large and mostly of non priniting chars. and wat if there are chars with negative ascii value?does ur logic handles this situation?I doubt it does, On Fri, Jul 29, 2011 at 8:21 PM, snehi jain wrote: > no problem .. > but can u find a bug in this technique .. > > > On Fri, Jul 29, 2011 at 8:17 PM, saurabh singh wrote: > >> ignore above status...i was looking at two problems >> simulataneously...posted on the wrong one... >> apologies...really its embarrasing. >> >> On Fri, Jul 29, 2011 at 8:11 PM, snehi jain wrote: >> >>> thanks .. >>> @siddhartha : i know the hashing technique ... and the problem of longer >>> strings can be reduced by subtracting a large constant value from the ASCII >>> value of all the characters .. and its not only with cubes even if u add >>> squares of ascii values ... i think it will work . >>> >>> @saurabh : in what context should i use bitfield ... >>> >>> >>> >>> On Fri, Jul 29, 2011 at 7:36 PM, saurabh singh wrote: >>> use a bitfield to optimize space On Fri, Jul 29, 2011 at 7:14 PM, Siddharth kumar < siddhartha.baran...@gmail.com> wrote: > #include > #include > > using namespace std; > > int main() > { > char str1[] = > "sdfgkirertyujvcheddirtyutrdfgrtyuigfkrertyuijhgfdshh"; > char str2[] = > "redirtyhgfdshhrtyrsujvcheierdutrdfgrtyuigfkdfgktyuij"; > int len1 = strlen(str1), len2 = strlen(str2); > > if(len1 != len2) > { > cout<<"Both are not anagram of each other."; > return 0; > } > else > { > int rep_char1[26] = {0}, rep_char2[26] = {0}, i; > > for(i=0; i { > rep_char1[str1[i]-'a']++; > rep_char2[str2[i]-'a']++; > } > > for(i=0; i<26; i++) > if(rep_char1[i] != rep_char2[i]) > { > cout<<"Both are not anagram of each other."; > return 0; > } > > cout<<"Both are anagram of each other."; > } > return 0; > } > > > > -- > Siddharth Kumar > BE 4th year, Computer Engineering > (Delhi College of Engineering) > +919711559760 > > -- > You received this message because you are subscribed to the Google > Groups "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Saurabh Singh B.Tech (Computer Science) MNNIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Algorithm Geeks" group. >>> To post to this group, send email to algogeeks@googlegroups.com. >>> To unsubscribe from this group, send email to >>> algogeeks+unsubscr...@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >> >> >> >> -- >> Saurabh Singh >> B.Tech (Computer Science) >> MNNIT ALLAHABAD >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Saurabh Singh B.Tech (Computer Science) MNNIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] doubt in checking if 2 strings are anagrams
no problem .. but can u find a bug in this technique .. On Fri, Jul 29, 2011 at 8:17 PM, saurabh singh wrote: > ignore above status...i was looking at two problems > simulataneously...posted on the wrong one... > apologies...really its embarrasing. > > On Fri, Jul 29, 2011 at 8:11 PM, snehi jain wrote: > >> thanks .. >> @siddhartha : i know the hashing technique ... and the problem of longer >> strings can be reduced by subtracting a large constant value from the ASCII >> value of all the characters .. and its not only with cubes even if u add >> squares of ascii values ... i think it will work . >> >> @saurabh : in what context should i use bitfield ... >> >> >> >> On Fri, Jul 29, 2011 at 7:36 PM, saurabh singh wrote: >> >>> use a bitfield to optimize space >>> >>> >>> On Fri, Jul 29, 2011 at 7:14 PM, Siddharth kumar < >>> siddhartha.baran...@gmail.com> wrote: >>> #include #include using namespace std; int main() { char str1[] = "sdfgkirertyujvcheddirtyutrdfgrtyuigfkrertyuijhgfdshh"; char str2[] = "redirtyhgfdshhrtyrsujvcheierdutrdfgrtyuigfkdfgktyuij"; int len1 = strlen(str1), len2 = strlen(str2); if(len1 != len2) { cout<<"Both are not anagram of each other."; return 0; } else { int rep_char1[26] = {0}, rep_char2[26] = {0}, i; for(i=0; i>>> { rep_char1[str1[i]-'a']++; rep_char2[str2[i]-'a']++; } for(i=0; i<26; i++) if(rep_char1[i] != rep_char2[i]) { cout<<"Both are not anagram of each other."; return 0; } cout<<"Both are anagram of each other."; } return 0; } -- Siddharth Kumar BE 4th year, Computer Engineering (Delhi College of Engineering) +919711559760 -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. >>> >>> >>> >>> -- >>> Saurabh Singh >>> B.Tech (Computer Science) >>> MNNIT ALLAHABAD >>> >>> >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Algorithm Geeks" group. >>> To post to this group, send email to algogeeks@googlegroups.com. >>> To unsubscribe from this group, send email to >>> algogeeks+unsubscr...@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > > > -- > Saurabh Singh > B.Tech (Computer Science) > MNNIT ALLAHABAD > > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] doubt in checking if 2 strings are anagrams
ignore above status...i was looking at two problems simulataneously...posted on the wrong one... apologies...really its embarrasing. On Fri, Jul 29, 2011 at 8:11 PM, snehi jain wrote: > thanks .. > @siddhartha : i know the hashing technique ... and the problem of longer > strings can be reduced by subtracting a large constant value from the ASCII > value of all the characters .. and its not only with cubes even if u add > squares of ascii values ... i think it will work . > > @saurabh : in what context should i use bitfield ... > > > > On Fri, Jul 29, 2011 at 7:36 PM, saurabh singh wrote: > >> use a bitfield to optimize space >> >> >> On Fri, Jul 29, 2011 at 7:14 PM, Siddharth kumar < >> siddhartha.baran...@gmail.com> wrote: >> >>> #include >>> #include >>> >>> using namespace std; >>> >>> int main() >>> { >>> char str1[] = "sdfgkirertyujvcheddirtyutrdfgrtyuigfkrertyuijhgfdshh"; >>> char str2[] = "redirtyhgfdshhrtyrsujvcheierdutrdfgrtyuigfkdfgktyuij"; >>> int len1 = strlen(str1), len2 = strlen(str2); >>> >>> if(len1 != len2) >>> { >>> cout<<"Both are not anagram of each other."; >>> return 0; >>> } >>> else >>> { >>> int rep_char1[26] = {0}, rep_char2[26] = {0}, i; >>> >>> for(i=0; i>> { >>> rep_char1[str1[i]-'a']++; >>> rep_char2[str2[i]-'a']++; >>> } >>> >>> for(i=0; i<26; i++) >>> if(rep_char1[i] != rep_char2[i]) >>> { >>> cout<<"Both are not anagram of each other."; >>> return 0; >>> } >>> >>> cout<<"Both are anagram of each other."; >>> } >>> return 0; >>> } >>> >>> >>> >>> -- >>> Siddharth Kumar >>> BE 4th year, Computer Engineering >>> (Delhi College of Engineering) >>> +919711559760 >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Algorithm Geeks" group. >>> To post to this group, send email to algogeeks@googlegroups.com. >>> To unsubscribe from this group, send email to >>> algogeeks+unsubscr...@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >> >> >> >> -- >> Saurabh Singh >> B.Tech (Computer Science) >> MNNIT ALLAHABAD >> >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Saurabh Singh B.Tech (Computer Science) MNNIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] doubt in checking if 2 strings are anagrams
thanks .. @siddhartha : i know the hashing technique ... and the problem of longer strings can be reduced by subtracting a large constant value from the ASCII value of all the characters .. and its not only with cubes even if u add squares of ascii values ... i think it will work . @saurabh : in what context should i use bitfield ... On Fri, Jul 29, 2011 at 7:36 PM, saurabh singh wrote: > use a bitfield to optimize space > > > On Fri, Jul 29, 2011 at 7:14 PM, Siddharth kumar < > siddhartha.baran...@gmail.com> wrote: > >> #include >> #include >> >> using namespace std; >> >> int main() >> { >> char str1[] = "sdfgkirertyujvcheddirtyutrdfgrtyuigfkrertyuijhgfdshh"; >> char str2[] = "redirtyhgfdshhrtyrsujvcheierdutrdfgrtyuigfkdfgktyuij"; >> int len1 = strlen(str1), len2 = strlen(str2); >> >> if(len1 != len2) >> { >> cout<<"Both are not anagram of each other."; >> return 0; >> } >> else >> { >> int rep_char1[26] = {0}, rep_char2[26] = {0}, i; >> >> for(i=0; i> { >> rep_char1[str1[i]-'a']++; >> rep_char2[str2[i]-'a']++; >> } >> >> for(i=0; i<26; i++) >> if(rep_char1[i] != rep_char2[i]) >> { >> cout<<"Both are not anagram of each other."; >> return 0; >> } >> >> cout<<"Both are anagram of each other."; >> } >> return 0; >> } >> >> >> >> -- >> Siddharth Kumar >> BE 4th year, Computer Engineering >> (Delhi College of Engineering) >> +919711559760 >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > > > -- > Saurabh Singh > B.Tech (Computer Science) > MNNIT ALLAHABAD > > > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] doubt in checking if 2 strings are anagrams
use a bitfield to optimize space On Fri, Jul 29, 2011 at 7:14 PM, Siddharth kumar < siddhartha.baran...@gmail.com> wrote: > #include > #include > > using namespace std; > > int main() > { > char str1[] = "sdfgkirertyujvcheddirtyutrdfgrtyuigfkrertyuijhgfdshh"; > char str2[] = "redirtyhgfdshhrtyrsujvcheierdutrdfgrtyuigfkdfgktyuij"; > int len1 = strlen(str1), len2 = strlen(str2); > > if(len1 != len2) > { > cout<<"Both are not anagram of each other."; > return 0; > } > else > { > int rep_char1[26] = {0}, rep_char2[26] = {0}, i; > > for(i=0; i { > rep_char1[str1[i]-'a']++; > rep_char2[str2[i]-'a']++; > } > > for(i=0; i<26; i++) > if(rep_char1[i] != rep_char2[i]) > { > cout<<"Both are not anagram of each other."; > return 0; > } > > cout<<"Both are anagram of each other."; > } > return 0; > } > > > > -- > Siddharth Kumar > BE 4th year, Computer Engineering > (Delhi College of Engineering) > +919711559760 > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Saurabh Singh B.Tech (Computer Science) MNNIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] doubt in checking if 2 strings are anagrams
#include #include using namespace std; int main() { char str1[] = "sdfgkirertyujvcheddirtyutrdfgrtyuigfkrertyuijhgfdshh"; char str2[] = "redirtyhgfdshhrtyrsujvcheierdutrdfgrtyuigfkdfgktyuij"; int len1 = strlen(str1), len2 = strlen(str2); if(len1 != len2) { cout<<"Both are not anagram of each other."; return 0; } else { int rep_char1[26] = {0}, rep_char2[26] = {0}, i; for(i=0; ihttp://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] doubt in checking if 2 strings are anagrams
for long strings, the values will be larger. a simple approach could be to store occurrence of each char of both the strings in an integer array of size 26 and if both the arrays are same then the two strings are anagrams. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] doubt in checking if 2 strings are anagrams
i was looking for techniques for checking if two words are anagrams ... and i did get a lot of techniques with sorting and then comparing ... but will this work... add the ASCII values of the characters and add the cube of ASCII value of the same characters .. if for 2 strings both the corresponding values are same then the strings are anagrams ... is there any problem with this approach?? thanks Snehi -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.