Skip to content

Commit

Permalink
refactor: Update AnagramSnippet.java (#186)
Browse files Browse the repository at this point in the history
* Update AnagramSnippet.java

Moved the array declaration after the if statement to minimise the unnecessary array creation in case the function returns from the if statement.

* Updated the Readme file for the related changes

---------

Co-authored-by: abd <abdullaha@phelix.ai>
  • Loading branch information
Abdullah8006 and abd authored Oct 8, 2023
1 parent fffb522 commit d4fefde
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -823,11 +823,13 @@ public static double calculateMatchRating(double firstPlayerRating, double secon
public boolean isAnagram(String s1, String s2) {
var l1 = s1.length();
var l2 = s2.length();
var arr1 = new int[256];
var arr2 = new int[256];

if (l1 != l2) {
return false;
}

var arr1 = new int[256];
var arr2 = new int[256];

for (var i = 0; i < l1; i++) {
arr1[s1.charAt(i)]++;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/string/AnagramSnippet.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ public class AnagramSnippet {
public static boolean isAnagram(String s1, String s2) {
var l1 = s1.length();
var l2 = s2.length();
var arr1 = new int[256];
var arr2 = new int[256];

if (l1 != l2) {
return false;
}

var arr1 = new int[256];
var arr2 = new int[256];

for (var i = 0; i < l1; i++) {
arr1[s1.charAt(i)]++;
arr2[s2.charAt(i)]++;
Expand Down

0 comments on commit d4fefde

Please sign in to comment.