Untitled

Run Settings
LanguageJava
Language Version
Run Command
//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. import java.util.Scanner; class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); String a = s.nextLine(); String b = s.nextLine(); Main m = new Main(); if(m.oneEdit(a,b)){ System.out.println("Yes One Edit Away"); }else{ System.out.println("No One Edit Away"); } } public boolean oneEdit(String first,String second){ int la= first.length(); int lb = second.length(); int diff = la-lb; boolean changes = false; if(diff > 1 || diff < -1 ){ return false; }else { String a =first.length()< second.length() ? first : second; String b =first.length()< second.length() ? second : first; int aCounter = 0; int bCounter = 0; while(aCounter < a.length() && bCounter < b.length()){ if(a.charAt(aCounter) != b.charAt(bCounter)){ if(changes){ return false; } changes = true; if(a.length() == b.length()){ aCounter++; } }else{ aCounter++; } bCounter++; } return true; } } }
Editor Settings
Theme
Key bindings
Full width
Lines