File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Approach 1
2
+ function Anagram ( str1 , str2 ) {
3
+ if ( str1 . length !== str2 . length ) {
4
+ return false ;
5
+ }
6
+ let str11 = str1 . split ( "" ) . sort ( ) . join ( "" ) ;
7
+ let str22 = str2 . split ( "" ) . sort ( ) . join ( "" ) ;
8
+ if ( str11 === str22 ) {
9
+ return true ;
10
+ }
11
+ return false ;
12
+ }
13
+
14
+ console . log ( Anagram ( "namit" , "timen" ) ) ;
15
+
16
+ // Approach 2
17
+ const CHAR = 256 ;
18
+ function Anagram ( str1 , str2 ) {
19
+ const charArray = new Array ( CHAR ) . fill ( 0 ) ;
20
+ if ( str1 . length !== str2 . length ) {
21
+ return false ;
22
+ }
23
+
24
+ for ( let i = 0 ; i < str1 . length ; i ++ ) {
25
+ charArray [ str1 . charCodeAt ( i ) ] ++ ;
26
+ charArray [ str2 . charCodeAt ( i ) ] -- ;
27
+ }
28
+
29
+ for ( let i = 0 ; i < CHAR ; i ++ ) {
30
+ if ( charArray [ i ] !== 0 ) return false ;
31
+ }
32
+
33
+ return true ;
34
+ }
35
+ console . log ( Anagram ( "triangle" , "integral" ) ) ;
You can’t perform that action at this time.
0 commit comments