Skip to content

Commit 6534117

Browse files
Merge pull request #350 from zafar-hussain/anagramBranch
Adding Check two strings are anagrams - in Racket - for issue #53
2 parents 007d2ac + 15e200f commit 6534117

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

problems/Anagram/isAnagram?.rkt

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#lang racket
2+
;Check weather the given two strings are anagrams of each other, i.e weather they contain the same letter.
3+
;
4+
;Example :
5+
;Input: abc & cab //abc is anagrams with cab, bac, cab..
6+
;Output: True
7+
8+
;; string -> boolean
9+
;; retruns true of given strings have the same characters
10+
11+
;; code
12+
;(define (isAnagram? s1 s2) true) ; stub
13+
14+
(define (isAnagram? s1 s2)
15+
(local [(define (isAnagram? s1 s2) ; helper function
16+
(cond
17+
[(and (empty? s1) (empty? s2)) true] ; basecase for recursion
18+
[else
19+
(and ; all the characters must match
20+
(equal? (first s1) (first s2))
21+
(isAnagram? (rest s1) (rest s2)))]))]
22+
(isAnagram? (sort (string->list s1) (lambda (a b) (> (char->integer a) (char->integer b)))) ; turning the string into a
23+
(sort (string->list s2) (lambda (a b) (> (char->integer a) (char->integer b))))))) ; sorted list
24+
25+
26+
27+
28+
29+
;; testing examples
30+
;(equal? (isAnagram? "" "") true) ; basecase
31+
;(equal? (isAnagram? "abc" "cab") true)
32+
;(equal? (isAnagram? "cat" "dog") false)

0 commit comments

Comments
 (0)