Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Feature/anagram py #114

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions String Algorithms/Anagram/Anagram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include<iostream>
#include <bits/stdc++.h>

using namespace std;

bool Anagram (string str1, string str2){
int n1 = str1.length();
int n2 = str2.length();

if (n1 != n2) return false;

sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());

for (int i = 0; i < n1; i++)
if (str1[i] != str2[i])
return false;
return true;
}

int main() {
string str1 = "listen";
string str2 = "silent";

// Note that each letter need to be in same case. Otherwise, use toUpper Function before adding to function.

cout << (Anagram(str1, str2) ? "Anagram" : "Not Anagram");

}
24 changes: 24 additions & 0 deletions String Algorithms/Anagram/Anagram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def anagram(str1, str2):
n1 = len(str1)
n2 = len(str2)

if n1 != n2:
return 0

str1 = sorted(str1)
str2 = sorted(str2)

for i in range(0, n1):
if str1[i] != str2[i]:
return 0
return 1


# Driver code
str1 = "listen"
str2 = "silent"

if anagram(str1, str2):
print("Anagram")
else:
print("Not Anagram")