Input: s – “abcde”, t – “abced”
Output = true
Input s – “abcde”, t – “abcfed”
Output – false
Below are two solutions to this problem.
Method 1: Sort both the strings and then compare the strings. Time Complexity is O(nlogn).
bool anagrams(string s, string t) { return s.sort() == t.sort() }
Method 2: Check if the two strings have same count for each character.
1. Create an array of size 256 initialized with 0′s.
2. For first string increment count of character in count array.
3. For second string decrement the count of character from count array.
4. Repeat steps 2 and 3 till we reach end of any string.
5. Check if array contains only zero then strings are anagram otherwise not.
bool anagrams(string s,string t) { if (s.length() ! = t.length()) return false; int letters[256] = {0}; for(int i=0;i<s.length();i++) { letters[s[i]]++; letters[t[i]]--; } for (int i = 0; i < 256; i++) { if(letters[i]) return false; } return true; }
Time Complexity : O(n)
One more O(N) and Space complexity O(1) approach:
take XOR of both the strings and if output is zero , string are anagram to each other else not.
Naveen your solution will not work.
lets take example
S1= “aaaa”;
S2= “bbbb”;
now apply your algo. here S1 and S2 are strings.
Amit
Here is the complete solution
Sorry Naveen,
Your solution won’t work for input strings adad and bcbc. Here sum of both strings are same but they are not anagram.
Pingback: Find Anagrams in array of Strings
what If two string have different lenght?
Sample Input 1
tea
slate
Sample Output1
ate
will it work?
Do yoou mind iff I quote a few of your articles as long
as I provide credit and sources back to your site?
My blog site is in the very same area of interest as yours and my users would truly benefit frm a loot of the information you provide here.
Please let me know if this ok with you. Many thanks!
public class Anagram {
public static void main(String[] args) {
String s1=”asdfas” ;
String s2=”asdfas”;
char [] arr=new char[256];
System.out.println();
for(int i=0;i<s1.length();i++)
{
arr[s1.charAt(i)]++;
}
for(int i=0;i<s2.length();i++)
{
arr[s2.charAt(i)]–;
}
int sum=0;
for(int i=0;i<256;i++)
{
sum += arr[i];
if(arr[i]<0)
break;
}
if(sum==0)
System.out.println("true");
else
System.out.println("false");
}
}
public class Anagram 1{
public static void main(String[] args) {
String s1=”asdfas” ;
String s2=”asdfas”;
char [] arr=new char[256];
System.out.println();
for(int i=0;i<s1.length();i++)
{
arr[s1.charAt(i)]++;
}
for(int i=0;i<s2.length();i++)
{
arr[s2.charAt(i)]–;
}
int sum=0;
for(int i=0;i<256;i++)
{
sum += arr[i];
if(arr[i]<0)
break;
}
if(sum==0)
System.out.println("true");
else
System.out.println("false");
}
}
import java.util.*;
public class AnagramOrNot
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String s,t;
System.out.println(“Enter the first string”);
s = scan.nextLine();
System.out.println(“Enter the second string”);
t = scan.nextLine();
char[] chrs = s.toCharArray();
char[] chrt = t.toCharArray();
int count=0;
if(s.length()==t.length())
{
for(int i=0;i<s.length();i++)
{
for(int j=0;j<t.length();j++)
{
if(chrs[i]==chrt[j])
{
count++;
}
else
continue;
}
}
if(count==s.length() || count==t.length())
{
System.out.println("They are anagrams");
}
else
System.out.println("They are not anagrams");
}
else
System.out.println("The words lengths do not match so they are not anagrams");
}
}
using System;
public class AreAnagrams{
public static bool AreStringsAnagrams(string a, string b){
char[] s1 = a.ToCharArray();
char[] s2 = b.ToCharArray();
Array.Sort(s1);
Array.Sort(s2);
Console.WriteLine(s1);
Console.WriteLine(s2);
string ss1 = new string(s1);
string ss2 = new string(s2);
if (ss1.CompareTo(ss2) == 0)
return true;
return false;
}
public static void Main(string[] args){
Console.WriteLine(AreStringsAnagrams(“asdfg”, “qwerty”));
Console.ReadKey(true);
}
}
NoOne’s Code actually works.
Tarun you are the biggest asshole ever.You dont even know how to write code.Why ypu guys waste yours and others time here…
Hellow my name is Martinsardy. Wery proper art! Thx