Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.5 One Away #5

Open
lpatmo opened this issue Nov 30, 2018 · 5 comments
Open

1.5 One Away #5

lpatmo opened this issue Nov 30, 2018 · 5 comments

Comments

@lpatmo
Copy link

lpatmo commented Nov 30, 2018

One Away: There are three types of edits that can be performed on strings: insert a character,
remove a character, or replace a character. Given two strings, write a function to check if they are
one edit (or zero edits) away.

EXAMPLE
pale, ple -> true
pales, pale -> true
pale, bale -> true
pale, bake -> false

@megantaylor
Copy link

https://repl.it/@megantaylor/OneAway

function oneAway(str1, str2) {
  if (typeof str1 !== 'string' || typeof str2 !== 'string') return false;
  if (str1 === str2) return true;
  let srtr1len = str1.length;
  let str2len = str2.length;
  if (Math.abs(srtr1len - str2len) > 1) return false;

  let count = 0; // Count of edits
  let i = 0;
  let j = 0;

  while (i < srtr1len && j < str2len) {
    // If current characters don't match
    if (str1[i] != str2[j]) {
      if (count == 1) return false;

      // If length of one string is
      // more, then only possible edit
      // is to remove a character
      if (srtr1len > str2len) i++;
      else if (srtr1len < str2len) j++;
      //Iflengths of both strings is same
      else {
        i++;
        j++;
      }

      // Increment count of edits
      count++;
    } // If current characters match
    else {
      i++;
      j++;
    }
  }

  if (i < srtr1len || j < str2len) count++;

  return count == 1;
}

@lpatmo
Copy link
Author

lpatmo commented Nov 30, 2018

https://repl.it/@lpatmo/SleepyToughRobodoc

function oneAway(str1, str2) {
  //Check if same string. if yes, return true.
  //Keep a "differences" counter.
  //If same length, loop through str1 and str2 (comparing first character to first char, etc.) and keep a tab on the number of differences. 
  //Check if lengths between the two strings is off by one
    //Loop through longer string, compare each char until you find a difference. Then insert the difference into the shorter string and continue looping. If you still find a difference after that, return false.
  //If off by more than one ("else"), then return false. 
  //If the total number of differences is less than two (0 or 1), return true. Otherwise, return false.
  if (str1 === str2) {
    return true;
  }
  let differences = 0;
  if (str1.length === str2.length) {
    for (let i = 0; i < str1.length; i++) {
      if (str1[i] !== str2[i]) {
        differences++;
      }
    }
  }
  if (Math.abs(str1.length - str2.length) > 1) {
    console.log('Lengths are too unequal, so there is definitely more than one difference')
    return false;
  } else {
    let shorterStr = str1.length < str2.length ? str1 : str2;
    let longerStr = str1.length > str2.length ? str1 : str2;
    //pale pld
    //pale pXld
    for (let i = 0; i < longerStr.length; i++) {
      if (longerStr[i] !== shorterStr[i]) {
        console.log(shorterStr)
        shorterStr = shorterStr.slice(0,i) + 'X' + shorterStr.slice(i);
        differences++;
      }
    }
  } 
  console.log('Number of differences is', differences)
  return differences < 2; // or differences == 1

}
console.log(oneAway("pale", "pld"))
console.log(oneAway("pale", "bakeasd"))

@jtkaufman737
Copy link

jtkaufman737 commented Dec 1, 2018

https://repl.it/@jtkaufman737/AllBewitchedBooleanvalue

function testsOneAway(strOne, strTwo){
  let irregularities = 0; 
  strOne = Array.from(strOne), strTwo = Array.from(strTwo);
  let shortString = strOne, longString = strTwo;

  if(strOne.length > strTwo.length){
    shortString = strTwo;
    longString = strOne;
  } 

  for(let i=0; i < shortString.length; i++) {
    if(irregularities < 2) {
      
      // handle same length 
      if(strOne.length == strTwo.length) {
        if(strOne[i] !== strTwo[i]) {
          irregularities += 1;
        } 
      } else { // handle uneven length
        if(shortString[i] != longString[i]) {
         irregularities += 1;
         longString.splice(i,1);   
       }
      }
    } else {
      break;
    }
  }

  if(irregularities < 2) {
    console.log("Great, you passed the one-away test!");
  } else {
    console.log("Sorry, these failed the one-away test")
  }

}


testsOneAway("pale","patt"); // should fail 
testsOneAway("pale","ple") // should pass 
testsOneAway("pale","bake") // should fail
testsOneAway("cake","bake") //should pass 
testsOneAway("pa le","pale") // should pass

/*
pale, ple -> true
pales, pale -> true
pale, bale -> true
pale, bake -> false
MORE TEST CASES
what if an argument is not a string? (false)
what if the two strings are exactly the same? (true?)
_this is not about permutations *at all*_
*/

@rmorabia
Copy link
Owner

rmorabia commented Dec 1, 2018

https://repl.it/@rmorabia/EcstaticImpureBytecode

const oneEdit = (str1, str2) => {
  str1 = str1.split("");
  str2 = str2.split("");
  let similarLetters = 0;
  let isItFalse = true;
  if (
    str1.length === str2.length ||
    str1.length - 1 === str2.length ||
    str1.length + 1 === str2.length
  ) {
    for (let i = 0, z = 0; i < str1.length, z < str2.length; i++, z++) {
      if (str1[i] === str2[z]) {
        similarLetters++;
      } else if (str1[i] !== str2[z]) {
        if (isItFalse === false) {
          return false;
        } else {
          i--;
          isItFalse = false;
        }
      }
    }
    if (similarLetters >= str1.length - 1) {
      return true;
    } else {
      return false;
    }
  } else {
    return false;
  }
};

console.log(oneEdit("cool", "cooll"));
console.log(oneEdit("cool", "ccool"));
console.log(oneEdit("cool", "cobol"));
console.log(oneEdit("cool", " cool"));
console.log(oneEdit("cool", "coo"));
console.log(oneEdit("cool", "coolll"));
console.log(oneEdit("cool", "c00l"));
console.log(oneEdit("cool", "co"));

@mcsmithers
Copy link

mcsmithers commented Dec 3, 2018

My solution

function oneLetter(stringOne, stringTwo) {
  if (stringOne.match(/^([a-z\(\)]+)$/i) && stringTwo.match(/^([a-z\(\)]+)$/i)) {
    if (stringOne.length - stringTwo.length === 1 || stringTwo.length - stringOne.length === 1) {
      return true;
    } else if (stringOne.length === stringTwo.length) {
      if (stringOne === stringTwo) {
        return false;
      } else {
        for (let letter = 0; letter < stringOne.length; letter++) {
          const testStringLetter = stringOne.charAt(letter);
          const comparisonStringLetter = stringTwo.charAt(letter);
          if (testStringLetter === comparisonStringLetter) {
            console.log(testStringLetter, ' matches with ', comparisonStringLetter);
          } else {
            console.log('Found the letter difference- ', testStringLetter, ' differs from ', comparisonStringLetter);
          }
        }
      }
    } else {
      return false;
    }
  } else {
    console.log('Has to be alphabet only');
  }
}

// Test time!
const testString = 'catch';
const comparingStringOne = 'cat';
oneLetter(testString, comparingStringOne);
const comparingStringTwo = 'watch';
oneLetter(testString, comparingStringTwo);
const comparingStringThree = 'c';
oneLetter(testString, comparingStringThree);
const comparingStringFour = 'catcher';
oneLetter(testString, comparingStringFour);
const comparingStringFive = 'c@tch';
oneLetter(testString, comparingStringFive);
const comparingStringSix = 'catch';
oneLetter(testString, comparingStringSix);
const comparingStringSeven = 'cath';
oneLetter(testString, comparingStringSeven);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants