Compare Strings by Sum of Chars in Java
The challenge
Compare two strings by comparing the sum of their values (ASCII character code).
- For comparing treat all letters as UpperCase
nullshould be treated as empty strings- If the string contains other characters than letters, treat the whole string as it would be empty
Your method should return true, if the strings are equal and false if they are not equal.
Examples:
"AD", "BC" -> equal
"AD", "DD" -> not equal
"gf", "FG" -> equal
"zz1", "" -> equal (both are considered empty)
"ZzZz", "ffPFF" -> equal
"kl", "lz" -> not equal
null, "" -> equal
The solution in Java code
Option 1:
public class Solution {
public static boolean compare(String s1, String s2) {
if (s1==null || s2==null) return true;
s1 = s1.replaceAll(" ", "").toUpperCase().trim();
s2 = s2.replaceAll(" ", "").toUpperCase().trim();
if (s1.length()==0 || s2.length()==0) return true;
int s1v = 0;
int s2v = 0;
for(int i=0;i<s1.length(); i++) {
if (!Character.isAlphabetic(s1.charAt(i))) return true;
s1v += (int) s1.charAt(i);
}
for(int j=0;j<s2.length(); j++) {
if (!Character.isAlphabetic(s2.charAt(j))) return true;
s2v += (int) s2.charAt(j);
}
return s1v==s2v;
}
}
Option 2:
public class Solution {
public static boolean compare(String s1, String s2) {
if (s1 == null || !s1.matches("[a-zA-Z]+")) s1 = "";
if (s2 == null || !s2.matches("[a-zA-Z]+")) s2 = "";
return s1.toUpperCase().chars().sum() == s2.toUpperCase().chars().sum();
}
}
Option 3:
class Solution {
static boolean compare(String s1, String s2) {
return (s1 != null && s1.matches("[a-zA-Z]+") ? s1.toUpperCase().chars().sum() : 0)
== (s2 != null && s2.matches("[a-zA-Z]+") ? s2.toUpperCase().chars().sum() : 0);
}
}
Test cases to validate our solution
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class SolutionTests {
@Test
public void BasicTests() {
assertEquals("'AD' vs 'BC'", true, Solution.compare("AD", "BC"));
assertEquals("'AD' vs 'DD'", false, Solution.compare("AD", "DD"));
assertEquals("'gf' vs 'FG'", true, Solution.compare("gf", "FG"));
assertEquals("'Ad' vs 'DD'", false, Solution.compare("Ad", "DD"));
assertEquals("'zz1' vs ''", true, Solution.compare("zz1", ""));
assertEquals("'ZzZz' vs 'ffPFF'", true, Solution.compare("ZzZz", "ffPFF"));
assertEquals("'kl' vs 'lz'", false, Solution.compare("kl", "lz"));
assertEquals("'[null]' vs ''", true, Solution.compare(null, ""));
assertEquals("'!!' vs '7476'", true, Solution.compare("!!", "7476"));
assertEquals("'##' vs '1176'", true, Solution.compare("##", "1176"));
}
@Test
public void RandomTests() {
for(int i=0; i < 40; i++) {
char letter1 = (char)(Math.random() * 26 + 65);
char letter2 = '1';
do {
letter2 = (char)(Math.random() * 26 + 65);
}
while(letter1 == letter2);
if(Math.random() < 0.5) {
assertEquals(true, Solution.compare(new String(new char[5]).replace('\0',letter1).toUpperCase() + letter2, letter2 + new String(new char[5]).replace('\0',letter1).toLowerCase()));
}
if(Math.random() < 0.5) {
assertEquals(false, Solution.compare(new String(new char[4]).replace('\0', letter1).toUpperCase() + letter2 + letter2, letter2 + new String(new char[4]).replace('\0', letter1).toLowerCase()));
}
}
}
}