OneCompiler

Frequency counter:: find two string have same char and including repeated chars

149

Frequency counter

Following is sample javascript code.

//
// triangle
// integral
//

function same(str1, str2){
  let frequency = {str1: {}, str2:{}, count: 0}
  if(str1.length !== str2.length) {
    return false
  }
  
  
  for(let i = 0; i < str1.length; i++) {
    if(frequency.str1[str1[i]]) {
      frequency.str1[str1[i]] += 1
    } else {
      frequency.str1[str1[i]] = 1
    }
     if(frequency.str2[str2[i]]) {
      frequency.str2[str2[i]] += 1
    } else {
      frequency.str2[str2[i]] = 1
    }
    
  
  }
  
  console.log(frequency)
  
  for(let j = 0; j < str1.length; j++) {
    // console.log
    if(frequency.str1[str1[j]] === frequency.str2[str1[j]]){
      frequency.count +=1
    } 
  }
  
  console.log( frequency )
  return frequency.count === str1.length ? true : false
}

console.log(same("trianglee", "integrale"))