File tree Expand file tree Collapse file tree 1 file changed +11
-10
lines changed
src/main/java/hackerrank/java Expand file tree Collapse file tree 1 file changed +11
-10
lines changed Original file line number Diff line number Diff line change 1
1
package hackerrank .java ;
2
2
3
- import hackerrank .java .Anagrams ;
4
-
5
3
import java .util .Arrays ;
4
+ import java .util .Locale ;
6
5
7
6
public class Anagrams {
8
7
String first ;
@@ -15,20 +14,22 @@ public Anagrams(String first, String second) {
15
14
16
15
public boolean isAnagram () {
17
16
18
- if (first .length () != second .length ())
17
+ if ( first .length () != second .length () ) {
19
18
return false ;
20
-
21
- // sort
22
- char [] firstCharArr = first .toUpperCase ().toCharArray ();
23
- char [] secondCharArr = second .toUpperCase ().toCharArray ();
19
+ }
20
+ Locale locale = Locale .ENGLISH ;
21
+ String firstUpper = first .toUpperCase (locale );
22
+ String secondUpper = second .toUpperCase (locale );
23
+ char [] firstCharArr = firstUpper .toCharArray ();
24
+ char [] secondCharArr = secondUpper .toCharArray ();
24
25
Arrays .sort (firstCharArr );
25
26
Arrays .sort (secondCharArr );
26
27
27
- // compare all the characters
28
- for (int i =0 ; i <first .length (); i ++){
28
+ for (int i =0 ; i < first .length (); i ++) {
29
29
30
- if ( firstCharArr [i ] != secondCharArr [i ])
30
+ if ( firstCharArr [i ] != secondCharArr [i ] ) {
31
31
return false ;
32
+ }
32
33
}
33
34
34
35
return true ;
You can’t perform that action at this time.
0 commit comments