LeetCode Problem

161. One Edit Distance 🔒 Link to LeetCode 🔒[locked - premium question] Given two strings S and T, determine if they are both one edit distance apart. Insert one character: Add exactly one character at any position in string s to make it equal to string t. For example, "ab" → "adb" (insert 'd'). Delete one character: Remove exactly one character from string s to make it equal to string t. For example, "abc" → "ac" (delete 'b'). Replace one character: Change exactly one character in string s to a different character to make it equal to string t. For example, "abc" → "adc" (replace 'b' with 'd'). Examples of strings that are one edit distance apart:  "ab" and "acb" (insert 'c')  "cab" and "ab" (delete 'c')  "1203" and "1213" (replace '0' with '1') Examples of strings that are NOT one edit distance apart:  "ab" and "ab" (zero edits needed - they're already equal)  "ab" and "adb" (this would be one edit, so it IS one edit distance apart)  "ab" and "adcb" (requires two insertions) public boolean isOneEditDistance(String s, String t) { if(s==null || t==null) return false; int m = s.length(); int n = t.length(); if(Math.abs(m-n)>1){ return false; } int i=0; int j=0; int count=0; while(i < m && j< n){ if(s.charAt(i)==t.charAt(j)){ i++; j++; }else{ count++; if(count>1) return false; if(m>n){ i++; }else if(m < n){ j++; }else{ i++; j++; } } } if(i < m || j< n) { count++; } if(count==1) return true; return false; }