Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

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 1 or 0) 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.

share|improve this question
 
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? –  Tony H. 5 hours ago
 
Is this [code-golf] or a [popularity-contest]? It can't be both. –  Gareth 5 hours ago
 
Sorry, I have modified my questions to reflect both comments. –  Dave Chen 5 hours ago
 
So inequality is allowed? –  user80551 5 hours ago
 
If the strings are to be hardcoded more than once(each) , do i have to count their length? –  user80551 4 hours ago
show 5 more comments

21 Answers

Ruby, 11

s = 'string'
t = 'string'
!!s[t]&t[s]

Checks if each string is contained within the other.

share|improve this answer
1  
!(a<b||b<a) would be the same... –  David Herrmann 3 hours ago
add comment

JavaScript, 11

Strings have to be stored in a and b.

!(a>b||a<b)
share|improve this answer
1  
You are using inequality operators. –  hosch250 3 hours ago
 
No, I am using greater than and less than operators. Those were not forbidden in the contest. –  user2270404 2 hours ago
 
+1. This is going to work identically in many other languages too. –  Paul 2 hours ago
1  
I may be wrong but can't you remove one |? –  Danny 2 hours ago
add comment

Python 49 45 18 22 15

( + 3 if string variables are considered)

print {a:0,b:1}[a]

The string should be hard coded at the two occurrences of a and one occurrence of b surrounded by quotes.

a and b should be per-initialized to the strings.


Python shell, 9

( + 3 if string variables are considered)

{a:0,b:1}[a]

Output in shell

>>> a = 'string'
>>> b = 'string'
>>> {a:0,b:1}[a]
1
>>> a = 'string'
>>> b = 'stringgg'
>>> {a:0,b:1}[a]
0
>>> {'string':0,'string':1}['string']
1
>>> {'stringggg':0,'string':1}['stringggg']
0
>>> 

Explanation

Creates a dict(hash table) with the key of first and second string. If second string is the same, the value of first is replaced by that of second. Finally, we print the value of first key.

EDIT: OP allowed 0/1 instead of False/True as well as using per-initialized variables.

share|improve this answer
 
@manatwork Golfscript and perl below use 0/1, can't I use that? –  user80551 4 hours ago
 
@manatwork Done –  user80551 4 hours ago
 
I am counting 16 instead of 13, for your second solution. –  Abhijit 2 hours ago
 
@Abhijit the a and b are not to be included, the strings should be hard-coded there, that's why I added + 2*len(str1) + len(str2) + 6 (') –  user80551 2 hours ago
add comment

GolfScript (5 chars)

'string1''string1'].&,(

Fairly straightforward port of the PHP reference implementation. Leaves 0 (=false) on the stack if the strings are the same, or 1 (=true) if they're different.

share|improve this answer
add comment

coreutils: uniq -d

Just enter your two strings as the standard input of a pipe and uniq -d | grep -q . will print nothing but will have a return value of success or error. If you want to print the boolean, just replace with uniq -d | grep -c .

How many characters? I let you count; uniq -d|grep -q . with no extra spaces has 17 characters in it, but since the whole job is performed by uniq, I would say this solution is a 0-character one in... uniq own language!

Actually, uniq -d will print one line if the two strings are identical, and nothing if the are different.

share|improve this answer
add comment

C++, 63 58

const char* a = "string";
const char* b = "string";
int main(){while(*a&&*b&&!(*a^*b))++a,++b;return!(*a^*b);}
share|improve this answer
 
Could you get away with auto instead of const char*? –  aldo 2 hours ago
 
I think so yes, but since the rules say the string variables aren't counted towards the character count I didn't bother to golf them. –  mattnewport 1 hour ago
add comment

Python, 79

a=list(a)
b=list(b)
for i in range(len(a)):
 if ord(a[i])-ord(b[i])<0:
  print 'n'
share|improve this answer
 
The question has been edited: the strings should be hard-coded, though I think your approach is better (golfier). –  11684 5 hours ago
 
It will print n multiple times –  user80551 4 hours ago
 
At the cost of a few chars, I could make it better –  TheDoctor 4 hours ago
 
Your program does not return whether the strings are equal or not, although it could be changed trivially to suit the rules. –  hosch250 3 hours ago
add comment

Python - 52 chars without strings (76 with strings)

The in operator might be skirting the edges, but the rules disallow strict equality comparison, not similarity comparison.

a="string"
b="string"
x=a in b and not(len(a)-len(b)>0 or len(a)-len(b)<0)
share|improve this answer
add comment

grep 14 characters

Of course, I only count the grep code; the two strings are on two consecutive lines in the input (either a pipe or a file or even an interactive session).

$ echo -e 'string\nstring' | grep -cPzo "(?s)^(\N*).\1$"
1
$ echo -e 'string\nstring1' | grep -cPzo "(?s)^(\N*).\1$"
0
$ echo -e 'string1\nstring' | grep -cPzo "(?s)^(\N*).\1$"
0
share|improve this answer
add comment

Matlab: 12 chars (after the strings are in variables)

~(x*x'-y*y')

The code including assignments would be:

x='string1'
y='string2'
~(x*x'-y*y')
share|improve this answer
add comment

Perl, 24 bytes

$r=!(-1+keys{$a,1,$b,1})

The two strings are given in $a and $b. The result is stored in $r.

The trick is using a hash array. If the strings are equal, then only one hash entry is created.


A standalone variant that gets the string from STDIN and returns 0, if the strings match and 1 otherwise:

$h{<>}=$h{<>}=1;exit -1+keys%h
share|improve this answer
 
@DaveChen: Thanks, == is now removed. –  Heiko Oberdiek 4 hours ago
add comment

The very crazy way

Just for the fun, but many ways for making it fail if one thinks about it. More over, don't forget the strings will be EXECUTED by the shell.

$ echo -e 'string\nstring1' | sed -e '1s/^/#define /' | cpp | sh 2>/dev/null && echo true
$ echo -e 'string\nstring' | sed -e '1s/^/#define /' | cpp | sh 2>/dev/null && echo true
true
$ echo -e 'string1\nstring' | sed -e '1s/^/#define /' | cpp | sh 2>/dev/null && echo true

A good counter-example is comparing "string" as first string and "rm -Rf /" as a second string; just check as root and see: it will say "true" though both strings obviously aren't the same.

share|improve this answer
add comment

Python 2 - 50 (including strings)

a='string'
b='string'
len(''.join(set(a+b)))==len(a)

Output

True

Non-equal strings

a='string'
b='String'
len(''.join(set(a+b)))==len(a)

Output

False
share|improve this answer
2  
Can't use ==, even if you're not comparing the strings. –  Dave Chen 4 hours ago
 
You may wish to use not (len(''.join(set(a+b)))-len(a)) instead. –  ace 6 mins ago
add comment

Ruby, 21

Online version

Strings have to be stored in a and b:

!!(a+b=~/^(#{a})\1$/)

Checks if the concatenation of a and b contains a twice.

share|improve this answer
 
Insufficient. Gives true for a="string"; b="stringstring". You have to anchor the regular expression to the start of string too with ^. –  manatwork 4 hours ago
 
Indeed. Thanks! –  David Herrmann 3 hours ago
1  
I feel like =~ needs to be forbidden, or you can just do !!(a=~/^#{b}$/) –  histocrat 2 hours ago
 
Well, it's just an abbreviation for .match(x): !!(a+b).match(/^(#{a})\1$/) - but yes, it somehow is an equality operator... –  David Herrmann 2 hours ago
add comment

D 30

string a="somestring";
string b="somestring";

return !reduce!"a|b"(a[]^b[]);
share|improve this answer
 
I think !reduce! falls under the banned equality operators. –  hosch250 3 hours ago
 
it negates the integer result, no different than the C solution using the same xor trick –  ratchet freak 3 hours ago
add comment

APL (8)

{∧/⍺∊¨⍵}
  • ⍺∊¨⍵: for each combination of elements in and , see if the element from contains the element from . Since in a string these will all be single characters, and a string contains itself, this is basically comparing each pair of characters.
  • ∧/: take the logical and of all the values (if all the characters match, the strings are equal)
share|improve this answer
add comment

C/C++ 43

bool foo()
{
    char *a="string1", *b="string2";
    for(;!(*a-*b)&&*a++&&*b++;);return!(*a+*b);
}
share|improve this answer
add comment

Python 15 - 2( if you hard code the string in the solution) = 13

>>> s = 'string'
>>> t = 'string'
>>> 1 is len({s,t})
True
>>> s = 'string1'
>>> t = 'string2'
>>> 1 is len({s,t})
False

Python 17

>>> s = 'string'
>>> t = 'string'
>>> s in t and t in s
True
>>> s = 'string1'
>>> t = 'string2'
>>> s in t and t in s
False
share|improve this answer
add comment

Python - 11 (without the strings)

>>> a = 'ss'
>>> b = 's'
>>> a in b in a
False
share|improve this answer
add comment

Perl, 25 bytes

$r="$a$b"=~/^(\Q$a\E)\1$/

The input is expected in variables $a and $b. The result is stored in $r. It uses a regular expression to check, whether the concatenation "$a$b" is a duplicate of $a.

share
add comment

C#

var b="string";
var a="string";
return (a.Length==b.Length)&&Regex.IsMatch(a, b);
share|improve this answer
6  
I am afraid this counts as one of the forbidden functions. –  hosch250 3 hours ago
1  
Also there's an == in there too. –  Paul R 3 hours ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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