-
-
Notifications
You must be signed in to change notification settings - Fork 605
Expand file tree
/
Copy pathMetaStrings.java
More file actions
27 lines (25 loc) · 790 Bytes
/
MetaStrings.java
File metadata and controls
27 lines (25 loc) · 790 Bytes
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
package problems.medium;
/**
* Created by sherxon on 4/19/17.
*/
public class MetaStrings {
public static void main(String[] args) {
System.out.println(isMetaString("geeks", "keegs")); // true
System.out.println(isMetaString("rsting", "string")); // false
System.out.println(isMetaString("Converse", "Conserve")); //true
}
static boolean isMetaString(String a, String b){
if(a.length()!=b.length())return false;
long sum=0;
long sum2=0;
int diffCount=0;
for (int i = 0; i < a.length(); i++) {
int aa=a.charAt(i)-'a';
int bb=b.charAt(i)-'a';
sum+=aa;
sum2+=bb;
diffCount+= aa == bb ? 0 : 1;
}
return sum==sum2 && diffCount<=2;
}
}