style: format code (#4212)

close #4204
This commit is contained in:
acbin
2023-06-09 18:52:05 +08:00
committed by GitHub
parent ad03086f54
commit 00282efd8b
521 changed files with 5233 additions and 7309 deletions

View File

@ -12,24 +12,21 @@ import java.util.HashMap;
*/
public class Anagrams {
// 4 approaches are provided for anagram checking. approach 2 and approach 3 are similar but differ in running time.
// 4 approaches are provided for anagram checking. approach 2 and approach 3 are similar but
// differ in running time.
public static void main(String[] args) {
String first = "deal";
String second = "lead";
// All the below methods takes input but doesn't return any output to the main method.
Anagrams nm = new Anagrams();
System.out.println(
nm.approach2(first, second)
);/* To activate methods for different approaches*/
nm.approach2(first, second)); /* To activate methods for different approaches*/
System.out.println(
nm.approach1(first, second)
);/* To activate methods for different approaches*/
nm.approach1(first, second)); /* To activate methods for different approaches*/
System.out.println(
nm.approach3(first, second)
);/* To activate methods for different approaches*/
nm.approach3(first, second)); /* To activate methods for different approaches*/
System.out.println(
nm.approach4(first, second)
);/* To activate methods for different approaches*/
nm.approach4(first, second)); /* To activate methods for different approaches*/
/**
* OUTPUT :
* first string ="deal" second string ="lead"
@ -55,9 +52,9 @@ public class Anagrams {
char[] c = s.toCharArray();
char[] d = t.toCharArray();
Arrays.sort(c);
Arrays.sort(
d
);/* In this approach the strings are stored in the character arrays and both the arrays are sorted. After that both the arrays are compared for checking anangram */
Arrays.sort(d); /* In this approach the strings are stored in the character arrays and
both the arrays are sorted. After that both the arrays are compared
for checking anangram */
return Arrays.equals(c, d);
}
@ -72,8 +69,10 @@ public class Anagrams {
for (char c : a.toCharArray()) {
m[c - 'a']++;
}
// In this approach the frequency of both the strings are stored and after that the frequencies are iterated from 0 to 26(from 'a' to 'z' ). If the frequencies match then anagram message is displayed in the form of boolean format
// Running time and space complexity of this algo is less as compared to others
// In this approach the frequency of both the strings are stored and after that the
// frequencies are iterated from 0 to 26(from 'a' to 'z' ). If the frequencies match
// then anagram message is displayed in the form of boolean format Running time and
// space complexity of this algo is less as compared to others
for (char c : b.toCharArray()) {
n[c - 'a']++;
}
@ -90,7 +89,8 @@ public class Anagrams {
if (s.length() != t.length()) {
return false;
}
// this is similar to approach number 2 but here the string is not converted to character array
// this is similar to approach number 2 but here the string is not converted to character
// array
else {
int[] a = new int[26];
int[] b = new int[26];
@ -110,7 +110,9 @@ public class Anagrams {
if (s.length() != t.length()) {
return false;
}
// This approach is done using hashmap where frequencies are stored and checked iteratively and if all the frequencies of first string match with the second string then anagram message is displayed in boolean format
// This approach is done using hashmap where frequencies are stored and checked iteratively
// and if all the frequencies of first string match with the second string then anagram
// message is displayed in boolean format
else {
HashMap<Character, Integer> nm = new HashMap<>();
HashMap<Character, Integer> kk = new HashMap<>();
@ -126,22 +128,25 @@ public class Anagrams {
}
boolean approach5(String s, String t) {
if(s.length() != t.length()){
if (s.length() != t.length()) {
return false;
}
// Approach is different from above 4 aproaches.
// Here we initialize an array of size 26 where each element corresponds to the frequency of a character.
// Approach is different from above 4 aproaches.
// Here we initialize an array of size 26 where each element corresponds to the frequency of
// a character.
int[] freq = new int[26];
// iterate through both strings, incrementing the frequency of each character in the first string and decrementing the frequency of each character in the second string.
for(int i=0; i<s.length(); i++){
// iterate through both strings, incrementing the frequency of each character in the first
// string and decrementing the frequency of each character in the second string.
for (int i = 0; i < s.length(); i++) {
int pos1 = s.charAt(i) - 'a';
int pos2 = s.charAt(i) - 'a';
int pos2 = s.charAt(i) - 'a';
freq[pos1]++;
freq[pos2]--;
}
// iterate through the frequency array and check if all the elements are zero, if so return true else false
for(int i=0; i<26; i++){
if(freq[i] != 0){
// iterate through the frequency array and check if all the elements are zero, if so return
// true else false
for (int i = 0; i < 26; i++) {
if (freq[i] != 0) {
return false;
}
}