29
\$\begingroup\$

Your task is simple. Determine if one string equals the other (not address, the value) without the use of equality operators (such as ==, ===, or .equal()) or inequality (!=, !==) anything similar for other languages. This means anywhere! You may not use these operators anywhere in the code. You may however, you use toggles such as !exp as you're not directly comparing the exp != with something else.

In addition, you may not use any functions such as strcmp, strcasecmp, etc.

As for comparison operators (>=, <=, >, <), they are also disallowed. I realize that some answers include this, but I'd really like to see more answers that don't borderline the equality operator.


An example using PHP is shown:

<?php

$a = 'string';
$b = 'string';

$tmp = array_unique(array($a, $b));

return -count($tmp) + 2;

Simply return true or false (or something that evaluates in the language to true or false like 0 or 1) to indicate if the strings match. The strings should be hardcoded seen in the above example. The strings should not be counted in the golf, so if you declare the variable before hand, don't count the declaration.

\$\endgroup\$
  • 1
    \$\begingroup\$ Is it necessary to output the result, or simply write a function to return a bool? If writing a complete program is required, that could make-or-break answers in languages with (relatively) significant boilerplate to create a functioning executable like Java and C# (such is the nature of the beast, but this challenge has little in the way of concrete guidelines, leaving much to interpretation/choice). And how are we to take the strings? Hardcoding, reading from STDIN, pass as command-line arguments? \$\endgroup\$ – Tony Ellis Mar 12 '14 at 14:23
  • \$\begingroup\$ Is this [code-golf] or a [popularity-contest]? It can't be both. \$\endgroup\$ – Gareth Mar 12 '14 at 14:23
  • \$\begingroup\$ Sorry, I have modified my questions to reflect both comments. \$\endgroup\$ – Dave Chen Mar 12 '14 at 14:32
  • \$\begingroup\$ So inequality is allowed? \$\endgroup\$ – user80551 Mar 12 '14 at 14:41
  • \$\begingroup\$ If the strings are to be hardcoded more than once(each) , do i have to count their length? \$\endgroup\$ – user80551 Mar 12 '14 at 15:13

82 Answers 82

1 2 3
0
\$\begingroup\$

C#

I already posted the answer But just found a number of ways in C#

 var b = "abcdef";
 var a = "abcdef";
 // the best one is this.
 bool repace = a.Replace(b, "").Length < 1 && b.Replace(a, "").Length < 1;
 bool contsins = ("a" + a).Contains(b) && ("b" + b).Contains(a);
\$\endgroup\$
  • \$\begingroup\$ forget to mention considering string are not empty. \$\endgroup\$ – sm.abdullah Mar 13 '14 at 18:42
0
\$\begingroup\$

C#, pretty straightforward.

static bool equal(string s1, string s2)
{
    int i = 0;
    try { while (true) if (!isZero((int)s1[i] - (int)s2[i++])) return false; }
    catch { }
    return isEnd(s1, i) && isEnd(s2, i);
}

static bool isEnd(string s, int i)
{
    try { char c = s[i]; return false; }
    catch { }
    return true;
}

static bool isZero(int x)
{
    try { int y = 1 / x; return false; }
    catch { }
    return true;
}
\$\endgroup\$
0
\$\begingroup\$

C, 48 chars

c(char*a,char*b){return*a++-*b?0:!*b++||c(a,b);}

Recursion is often shorter than a loop.
Subtraction easily replaces a comparison.

\$\endgroup\$
0
\$\begingroup\$

Lua, 147

a="string" b="string" y=string.byte s=string.sub m=math.abs c=1 for i=1,math.max(#a,#b) do d=y(s(a,i)) e=y(s(b,i))if not((d and e)and(m(d) - m(e)))then c=0 break end end

Assumes a and b are the strings. c should be 1 (true)

\$\endgroup\$
0
\$\begingroup\$

Python 56

a = "string"
b = "string"

not any(ord(x)*len(a)-ord(y)*len(b) for x,y in zip(a,b))

A bit longer than the shortest Python examples, but probably about the best I can do without using in or set notation (the former should probably be excluded, the latter has already been employed as succinctly as possible).

\$\endgroup\$
0
\$\begingroup\$

Rebol, 22

all[find a b find b a]

This will return none if strings are different.

Example usage in Rebol console:

>> a: "string"
== "string"

>> b: "string"
== "string"

>> c: "stringstring"
== "stringstring"

>> all [find a b find b a]
== "string"

>> all [find a c find c a] 
== none

By default Rebol is case insensitive. So use find/case to enforce stricter test:

>> b: "STRING"
== "STRING"

>> all [find a b find b a] 
== "STRING"

>> all [find/case a b find/case b a]
== none
\$\endgroup\$
0
\$\begingroup\$

Javascript

Where x = z and x != y:

if( x.match(RegExp(z)) ) // Evaluates to True


if( x.match(RegExp(y)) ) // Evaluates to False
\$\endgroup\$
  • 1
    \$\begingroup\$ What if z === 'Abc.efg' and x === 'Abcdefg'? \$\endgroup\$ – Not that Charles Mar 27 '14 at 22:10
0
\$\begingroup\$

Bash, 14

Assuming strings are in A and B, then

[ -z ${A/$B} ]

will return a true (0) or false (1) value in $?.

How do I use it?

A="string";B="string";[ -z ${A/$B} ]&&echo same 

How does it work?

${A/$B} replaces $B in $A with nothing. Only if $A=$B then result will be an empty string. [ -zn X ] tests for empty or null string and sets $? to 0 if empty.

Update: should be -z not -n in test.

\$\endgroup\$
0
\$\begingroup\$

Windows Batch (47)

@echo.>%1
@if exist %2 (exit /b 0)
@exit /b 1

This should be run in an empty directory each time ;)

It types a blank line into a file named with the first argument, and then checks if the second argument exists. For hardcoded strings, either replace the %1 and %2 or add @if "%1"=="" %0 string1 string2 to the top of the file.

\$\endgroup\$
  • \$\begingroup\$ Do you have a reference for your “errorlevel isn't 0 by default” comment? \$\endgroup\$ – manatwork Mar 20 '14 at 10:55
0
\$\begingroup\$

Java

import java.util.HashSet;
public class Equals {
    public static boolean equals(String s1, String s2){
        HashSet<String> sSet = new HashSet<>();
        sSet.add(s1);
        return !sSet.add(s2);
    }
    public static void main(String[] args) {
        System.out.println(equals("Apple","Tpple"));
    }
}
\$\endgroup\$
  • \$\begingroup\$ This can still be golfed a bit. Also you should include the bytecount in the title of your answer :] \$\endgroup\$ – Poke Mar 26 '17 at 2:04
0
\$\begingroup\$

perl, 20 bytes

Today I'm serving up perl two ways, saving a byte or two off the other perl solutions seen here. First, with hash slices, for 20 bytes:

@x{$a,$b}=();$_=%x%2

and then a port of the clever python solution above (also 20 bytes)

$_={$a,0,$b,1}->{$a}
\$\endgroup\$
0
\$\begingroup\$

C - 59

e(char*l,char*r){for(;*l&*r&&!(*l++^*r++););return!*l&!*r;}
\$\endgroup\$
0
\$\begingroup\$

Ruby (18)

This one is a script using the exit status instead of a function's return value: 0 is success, 1+ is failure.

exit$*.uniq.size-1

Usage:

$ ruby equality.rb a b
$ echo $?
1
$ ruby equality.rb a a
$ echo $?
0
\$\endgroup\$
0
\$\begingroup\$

R, 4

"string1"%in%"string2" equivalent to is.element("string1", c("string2"))

\$\endgroup\$
0
\$\begingroup\$

D - 34 27 bytes

!any!"a.length"(split(a,b))

Example use:

import std.stdio,std.string,std.algorithm;

void main() {
    auto a = "abcd";
    auto b = "abcd";

    writeln(!any!"a.length"(split(a,b)));
}

Output:

true
\$\endgroup\$
0
\$\begingroup\$

~-~! (No Comment) - 1

(might not be legal)

|string|-|string|

% if they're equal, anything else if they're not. There's an issue though; ~-~! only accepts % or ~ (0 or 1) for conditionals; if it's neither, it throws an error. So you'd have to use ==~ to actually be able to use it properly. I don't think there /is/ a way to do this in ~-~! without the equality operator if this isn't allowed.

\$\endgroup\$
0
\$\begingroup\$

Excel VBA, 124 Bytes

Subroutine that takes variant input a and b of the assumed type variant\string and outputs as a boolean variable to the VBE immediates window

Sub m(a,b)
c=1
For i=1To Len(a)
If Asc(Right(Left(a,i),1))-Asc(Right(Left(b,i),1)) Or Len(a)-Len(b) Then:c=0:Exit For
Next
Debug.?CBool(c)
End Sub
\$\endgroup\$
0
\$\begingroup\$

Operation Flashpoint scripting language, 7 bytes

a in[b]

Strings are stored in variables a and b (as allowed by the question).

Interestingly, even if direct comparison would be allowed, this would be the only valid solution, because a==b performs a case-insensitive comparison, whereas the built-in in uses case-sensitive comparison for strings internally.

\$\endgroup\$
0
\$\begingroup\$

[shell], 22 bytes (almost any shell (sh,bash,ksh,zsh, etc), certainly POSIX ones)

a=string
b=string

c=${a#"$b"}
echo ${#c}

Try it online!

Will output 0 if strings are equal, 1 if different. The less robust solution that doesn't deal with (*, ? and [ in $b) is two bytes shorter:

c=${a#$b}
echo ${#c}
\$\endgroup\$
0
\$\begingroup\$

PYTHON, 25

not bool(a.replace(b,''))

Explanation

An empty string is false in Python, so if we replace b with '' and check if it is empty that gives use true/false.

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to the site! As a tip not will automatically cast to bool in python so there is no need for the bool. However this answer doesn't work. If a is the string xx and b is the string x then the replacement will be empty but the two are not equal. \$\endgroup\$ – Post Rock Garf Hunter Dec 29 '19 at 15:46
  • \$\begingroup\$ If both a.replace(b,'') and b.replace(a,'') are empty then it will be equal so you can do something like not a.replace(b,"")+b.replace(a,""). \$\endgroup\$ – Post Rock Garf Hunter Dec 29 '19 at 15:51
-1
\$\begingroup\$

Java - 54

First Golf: Since all string's HashCode is the same with the same content, it can be useful.

(int)Math.abs(Math.signum(s.hashCode()-b.hashCode()));
\$\endgroup\$
  • 5
    \$\begingroup\$ This is wrong. Non-identical strings may have different hash codes. \$\endgroup\$ – Konrad Borowski Mar 13 '14 at 13:37
-1
\$\begingroup\$

C#

var b="string";
var a="string";
return  Regex.IsMatch(b, "^.{" + a.Length + "}$") && Regex.IsMatch(b,"^" + a + "$");
\$\endgroup\$
  • 9
    \$\begingroup\$ I am afraid this counts as one of the forbidden functions. \$\endgroup\$ – user10766 Mar 12 '14 at 15:53
  • \$\begingroup\$ awh.. I hope now it is fine return Regex.IsMatch("^" + a + "$", b); is it ? \$\endgroup\$ – sm.abdullah Mar 13 '14 at 13:38
  • \$\begingroup\$ According to your function, a = "string.!?" equals `b = "stringz". \$\endgroup\$ – CompuChip Mar 13 '14 at 14:33
  • \$\begingroup\$ again...... : P \$\endgroup\$ – sm.abdullah Mar 13 '14 at 16:27
1 2 3

Not the answer you're looking for? Browse other questions tagged or ask your own question.