-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOneAwayString.java
42 lines (38 loc) · 1.14 KB
/
OneAwayString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.*;
/*
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.
*/
public class OneAwayString{
public static void main(String[] args) {
System.out.println(getOneAway("pale","ple"));
System.out.println(getOneAway("pales","pale"));
System.out.println(getOneAway("pale","bale"));
System.out.println(getOneAway("pale","bae"));
System.out.println(getOneAway("pales","pales"));
}
public static boolean getOneAway(String s1, String s2){
HashMap<Character,Integer> map = new HashMap<>();
for(int i=0; i < s1.length() ; i++){
char c = s1.charAt(i);
map.put(c,1);
}
for(int i = 0; i <s2.length(); i++){
char c = s2.charAt(i);
if(map.containsKey(c)){
map.replace(c,map.get(c) -1);
}
}
int count = 0;
for(int a : map.values()){
if(a == 0){
count++;
}
}
if(count == s1.length()-1 || count== s1.length()){
return true;
}
return false;
}
}