Given two strings and a value k, find if the two strings are K-anagrams or not. Two strings are k-anagrams when they have same length of characters, and are anagram by changing at most k characters in a string.
When both strings have the same number of characters, go through each character in the first string and keep track of how many times each character appears. Go through each character in the second string. If a character is found in the first string, reduce its count. Find the total difference in character counts between the two strings. If the total difference is less than or equal to a given value "K," the strings are considered "k-anagrams." Otherwise, they are not.
output = False str1 = "pale" str2 = "bale" k = 1 m = {} if len(str1) == len(str2): for i in range(len(str1)): c = str1[i] if c not in m: m[c] = 0 m[c] = m[c] + 1 for i in range(len(str2)): c = str2[i] if c in m and m[c] > 0: m[c] = m[c] - 1 count = 0 for i in m: count += m[i] output = count <= k else: output = False print(output)
let output = false; const str1 = "pale"; const str2 = "bale"; const k = 1 const m = {}; if (str1.length == str2.length) { for (let i = 0; i < str1.length; i++) { const c = str1.at(i); if (!m[c]) { m[c] = 0; } m[c] = m[c] + 1; } for (let i = 0; i < str2.length; i++) { const c = str2.at(i); if (m[c] && m[c] > 0) { m[c] = m[c] - 1; } } let count = 0; for (let i in m) { count += m[i]; } output = count <= k; } else { output = false; } console.log(output);