HackerRank Strings : Making Anagrams Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

Great content! It is also possible to use one array :D

// Complete the makingAnagrams function below.
static int makingAnagrams(String s1, String s2) {
int values[] = new int[26];
for (int i = 0; i < s1.length(); i++) {
values[s1.charAt(i) - 'a']++;
}

for (int i = 0; i < s2.length(); i++) {
values[s2.charAt(i) -'a']--;
}

int removeChars = 0;
for (int i = 0; i < values.length; i++) {
removeChars += Math.abs(values[i]);
}

return removeChars;
}

megan.paffrath
Автор

//shorter way to increment array vals//
for (int i = 0; i < str.length(); i++) {
int position = str.charAt(i) -'a';
strArray[position]++;
}

MegaBezalel
Автор

Thank you for explaining! I saw a similar solution in the Discussion board, but didn't understand the subtracting 'a' part. This clears it up.

Firecloak
Автор

Can you share the hashmap code you started talking about in the beginning?

arnab_bhattacharya
Автор

Thanks for the advise about using alphabet letters array and for the explanation

HugoAyala
Автор

But why are we not considering uppercase letters...please explain.

rashidixit
Автор

please tell me why we have to use absolute function while subtracting

amanpal
Автор

nice explanation as well as programming skills..subbed

Holeecrap
Автор

a = input()
b = input()

hashone = [0 for x in range(26)]

for i in range (0, len(a)):
hashone [ord(a[i])-97] = hashone [ord(a[i])-97] + 1

for i in range (0, len(b)):
hashone [ord(b[i])-97] = hashone [ord(b[i])-97] - 1

count = 0

for i in range (26):
if hashone[i] < 0 or hashone[i] > 0:
count = count + abs(hashone[i])
print (count)

vikashvishnumurthy
Автор

Hey buddy, I understand the hashmap techniques in solving string related problems. However, I am finding it difficult to understand the 26 character technique. Could you or anyone point me to an article or video that would explain the concept in-depth and much better? thanks

ayoolao.
Автор

bro you are just awesome... totally impressed....

sayantanadhikari
Автор

This was my solution using StringBuilder and a concatenated for:
static int makeAnagram(String a, String b) {
int ts=a.length() + b.length();
StringBuilder nb= new StringBuilder(b);
for(int i=0; i<a.length(); i++){
for(int j=0; j<nb.length(); j++){
if(a.charAt(i)== nb.charAt(j)){
nb= nb.deleteCharAt(j);
ts -=2;
break;
}
}
}
return ts;

}

jocelinnejimennez
Автор

Easier solution :

static int makeAnagram(String a, String b) {
int i =0;
while (i <= a.length() - 1) {
char ch = a.charAt(i);
if {
a = a.replaceFirst(String.valueOf(ch), "");
b = b.replaceFirst(String.valueOf(ch), "");


}
else


i++;
}
return a.length() + b.length();
}

চলচ্চিত্রকথা
Автор

Using single count Array

static int makingAnagrams(String s1, String s2) {
int [] count = new int[26];
int res =0;
for(char ch: s1.toCharArray()){
count[ch-'a']++;
}
for(char ch: s2.toCharArray()){
count[ch-'a']--;
}
for(int num: count){
if(num !=0){
res+=Math.abs(num);
}
}
return res;
}

sangeethap.h.
Автор

You are Walter White of computer science.

codecartel
Автор

Im not getting & even your way of speaking is too fast

kusumam
Автор

I came across witha solution but it took me 30 minutes... and this just took you less than 5 minutes to crack it... holy wakamoly!

EduardoMartinez-dmpp
Автор

Holy shit, that's very over engineered.
You just have to remove what exists on both Strings, whatever is left is what should be deleted, so return that...

public static int makeAnagram(String a, String b) {
String a2 = a;
String b2 = b;

for(char c : a.toCharArray()){
b2 = b2.replaceFirst(String.valueOf(c), "");
}
for(char c : b.toCharArray()){
a2 = a2.replaceFirst(String.valueOf(c), "");
}

return a2.length()+b2.length();
}

CDERedEdits
Автор

This could be really straight forward in JS -


let string1Array = string1.split('');
let string2Array = string2.split('');
if === {
return true
} else {
return false
}

codiumfragile
welcome to shbcf.ru