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; }
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)
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
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
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(); }
This could be really straight forward in JS - let string1Array = string1.split(''); let string2Array = string2.split(''); if (JSON.stringify(string1Array.sort()) === JSON.stringify(string2Array.sort())) { return true } else { return false }
but if it's false, we need to know how many deletions are necessary so that it becomes an anagram. This doesn't really solve anything other than tell us whether or not it's an anagram.
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;
}
nicely figured out one more solution! thanks!
//shorter way to increment array vals//
for (int i = 0; i < str.length(); i++) {
int position = str.charAt(i) -'a';
strArray[position]++;
}
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.
Can you share the hashmap code you started talking about in the beginning?
Thanks for the advise about using alphabet letters array and for the explanation
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)
nice explanation as well as programming skills..subbed
bro you are just awesome... totally impressed....
please tell me why we have to use absolute function while subtracting
But why are we not considering uppercase letters...please explain.
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
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
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;
}
Easier solution :
static int makeAnagram(String a, String b) {
int i =0;
while (i
Im not getting & even your way of speaking is too fast
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!
You are Walter White of computer science.
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();
}
This could be really straight forward in JS -
let string1Array = string1.split('');
let string2Array = string2.split('');
if (JSON.stringify(string1Array.sort()) === JSON.stringify(string2Array.sort())) {
return true
} else {
return false
}
but if it's false, we need to know how many deletions are necessary so that it becomes an anagram. This doesn't really solve anything other than tell us whether or not it's an anagram.