File tree 1 file changed +32
-0
lines changed
1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
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)
You can’t perform that action at this time.
0 commit comments