0

I have the following string:

var strOfCharToSort = "azcdczbdxaaczdbbaazdz"

but I'm trying to get the count of the different substrings

for example:

let countofA = strOfCharToSort.filter { $0 == "a" }.count

and it works but I don't know what substrings are in the string I'm loading

I can sort the string:

strOfCharToSort = String(strOfCharToSort.sorted()) \\ result: aaaaabbbcccddddxzzzzz

But my question to guys there is a way to split the string when if finds a different substring?

I'll really appreciate you help.

user2924482
  • 8,380
  • 23
  • 89
  • 173

2 Answers2

0
let strOfCharToSort = "azcdczbdxaaczdbbaazdz"
let setOfChars = Set(strOfCharToSort)
let setOfCharsArray = Array(setOfChars).sorted()
let listOfSortedCharSubstrings = setOfCharsArray.map { (charachter) in
    return strOfCharToSort.filter { $0 == charachter }
}

This is a solution to get the sub strings of a sorted character array.

John Franke
  • 1,444
  • 19
  • 23
0

Try This

let StringOfChar = "azcdczbdxaaczdbbaazdz"
let SetOfAllChar = Set(StringOfChar)
for char in SetOfAllChar {
    let countofChar = StringOfChar.filter { $0 == char }.count
    print("Count of \(char) : \(countofChar)")
}

Output:

Count of d: 4
Count of z: 5
Count of c: 3
Count of b: 3
Count of a: 5
Count of x: 1
Vinu Jacob
  • 393
  • 2
  • 15