← Back to topics
Topic

Char swap 7th test case

j
jokermc1
Can i ask for 7th test case of Char Swap... thats the only one that doesnt work???
T
Tavo92
If you paste your code we can help you :)

(Do you gente TLE or WA?)
A
Al3kSaNdaR
Me to, I don't know what am I doing wrong. Please look at my code and give me some hint. ;)
T
Tavo92
I haven't seen your code, but the approach is quite straightforward:

Given a string s1 and s2:

For each letter i
s1[i] letter is hashed to s2[i]

Construct s3 using the hash, that is:
For each letter i
s3[i] = hash of letter s1[i]

Then check is s3 is equal to s2, if it's not equal then return false, else return true.

NOTE: You have to run this twice, the first run is s1, s2, and the next you have to swap their values.
A
Al3kSaNdaR
My idea is to calculate number of repeating for each letter and have two arrays [1..26] Cnt1 and Cnt2. I sorted them and compare them. If they are identical then output "DA" ( Yes ) , else output "NE" ( No ) .
j
jokermc1
hahahah aleksandar we have the same idea :D LOL
T
Tavo92
And why that should work (Well, I see it doesn't, but give me a prof because I don't understand the idea behind your solution).
t
tgudlek
I assign number to each letter:
abbcdb
011231

and I do that for both strings:
abbcdb
011231
gttvet
011231

After that, I just compare these two arrays.
A
Al3kSaNdaR
I don't know how to explain it, but it works ( just one case don't work ). It's similar with tgudlek's idea.
a
aleksa92
As I understand, you have not worried about positions of chars.
Try this case:
3
aab
bab
a
aleksa92
And it should print "NE", becouse word bab can not be made from aab.
D
Daniel93
it should print "NE".
A
Al3kSaNdaR
WTF, Ii must watch positions of chars in word?
a
aleksa92
I have done this task by a table of letters too. But instead of putting counts of letters, I've put which letter should be translated into other.
For example
char niz[26];
At the start it will have all values to 0, which means letters have no assigments yet. When i want to get letter into table, i have to check if letter in table is same as letter in word 2.
A
Al3kSaNdaR
Ok, i'll try to do it the other way. ;)
a
aleksa92
And when I want to check if word 1 can be make by word 2, only thing I need is to count occurances of letter in niz. If same letter is more than once in niz, then word 1 can not be made from word 2.