Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

added valid anagram function code #1310

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions algorithms/CPlusPlus/Arrays/valid-anagram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//Description - Given two strings s and t, return true if t is an anagram of s, and false otherwise.

//Time complexity - O(n logn)

//Approach - we sort the given strings, and we compare each elements of both the strings. If we get elements which are not equal, we return false


bool isAnagram(string s, string t) {
sort(s.begin(), s.end());
sort(t.begin(), t.end());

if(s.size()!=t.size()) return false;

for(int i = 0; i<s.size();i++)
{
if(s[i]!=t[i]) return false;
}
return true;
}