3

I was playing around on HackerEarth and I came across this issue.

What I try to do is to compare the strings and check if they have the same characters or not.

var string = ""

while let thing = readLine() 
{ 
string += thing + " "
}

var arrayStr = string.split(separator: " ").map{String(($0))}

var firstString = [String]()

var secondString = [String]()

var cas = arrayStr[0]

for i in 1..<arrayStr.count
{
if i % 2 != 0 
{
    firstString.append(String(arrayStr[i]))
}
else
{
    secondString.append(String(arrayStr[i]))
}
}
print(firstString) //["sumit", "ambuj", "abhi"]


print(secondString) //["mitsu", "jumba", "hibb"]

So, now you can see that the first index of firstString and secondString contains the same character, same for the second index, but not for the last one.

So, how can I compare them? I tried NSCharacter, but HackerEarth is not picking that up. Any ideas?

Rob13
  • 381
  • 8
  • 20
  • Possible duplicate of [How do I check in Swift if two arrays contain the same elements regardless of the order in which those elements appear in?](https://stackoverflow.com/questions/36714522/how-do-i-check-in-swift-if-two-arrays-contain-the-same-elements-regardless-of-th) – Roman Podymov Apr 23 '19 at 16:17

4 Answers4

12

If “multiplicity” counts (i.e. "aab" has the same characters as "aba", but not the same characters as "abb"), then

s1.sorted() == s2.sorted()

does the trick. If you don't care about the multiplicity, then just

Set(s1) == Set(s2)

Example:

let firstArray = ["sumit", "ambuj", "abhi", "aba"]
let secondArray = ["mitsu", "jumba", "hibb", "abb"]

for (s1, s2) in zip(firstArray, secondArray) {
    print(s1.sorted() == s2.sorted())
}

// true, true, false, false

for (s1, s2) in zip(firstArray, secondArray) {
    print(Set(s1) == Set(s2))
}

// true, true, false, true

For longer strings it might be more efficient to maintain a dictionary with the number of occurrences of each character in a string (similar to a NSCountedSet):

func characterCounts(_ s: String) -> [Character: Int] {
    return s.reduce(into: [:], { $0[$1, default: 0] += 1 })
}

and then compare the dictionaries:

characterCounts(s1) == characterCounts(s2)
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
3

An elegant way could be :

extension String {
    func haveSameCharecterSet(_ str : String) -> Bool {
            return self.sorted() == str.sorted()
    }
}

May be used like this :

var str1 = "ABCD"
var str2 = "CDBA"
print(str1.haveSameCharecterSet(str2));
//true
Rizwan
  • 3,324
  • 3
  • 17
  • 38
0

I found this solution faster if we deal with "complicated" strings.

func isCharactersTheSame(s1: String, s2: String) -> Bool {
    return Set(s1).symmetricDifference(Set(s2)).count == 0 
}

For example if we compare "aaaaaaaaa" and "a" for 100 times then s1.sorted() == s2.sorted() faster but in comparing "aaaaaaaaaa" and "a" Set(s1).symmetricDifference(Set(s2)).count == 0 will win

Nikolai Prokofev
  • 184
  • 3
  • 14
0
func sameNames(nameOne: String, nameTwo: String) -> Bool {
    var first = nameOne.sorted()
    var second = nameTwo.sorted()
    return first == second
}

sameNames(nameOne: "Pavel", nameTwo: "Maria")
NexusUA
  • 217
  • 1
  • 3
  • 13