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\$ – David 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

78 Answers 78

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\$
  • 1
    \$\begingroup\$ It doesn't work here: pastebin.com/mv9RLC9p \$\endgroup\$ – manatwork Mar 17 '14 at 9:41
  • \$\begingroup\$ @manatwork, thanks. should be -z. \$\endgroup\$ – philcolbourn Mar 20 '14 at 5:56
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\$
-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\$ – Hosch250 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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