Skip to content

Instantly share code, notes, and snippets.

@gazolla
Last active October 22, 2022 22:46
Show Gist options
  • Save gazolla/ec44f4638c84cb8fa29280716b5f6bf7 to your computer and use it in GitHub Desktop.
Save gazolla/ec44f4638c84cb8fa29280716b5f6bf7 to your computer and use it in GitHub Desktop.
import Foundation
/*
1108. Defanging an IP Address
Given a valid (IPv4) IP address, return a defanged version of that IP address.
A defanged IP address replaces every period "." with "[.]".
Example 1:
Input: address = "1.1.1.1"
Output: "1[.]1[.]1[.]1"
Example 2:
Input: address = "255.100.50.0"
Output: "255[.]100[.]50[.]0"
*/
func defangingIPAdress(_ str:String)->String{
let aStr = Array(str)
var result = ""
for i in 0...aStr.count - 1{
if aStr[i] == "." {
result += "[.]"
} else{
result += String(aStr[i])
}
}
return result
}
let add = "255.100.50.0"
print(defangingIPAdress(add))
/*
2114. Maximum Number of Words Found in Sentences
A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
You are given an array of strings sentences, where each sentences[i] represents a single sentence.
Return the maximum number of words that appear in a single sentence.
Example 1:
Input: sentences = ["alice and bob love leetcode", "i think so too", "this is great thanks very much"]
Output: 6
Explanation:
- The first sentence, "alice and bob love leetcode", has 5 words in total.
- The second sentence, "i think so too", has 4 words in total.
- The third sentence, "this is great thanks very much", has 6 words in total.
Thus, the maximum number of words in a single sentence comes from the third sentence, which has 6 words.
*/
func mostWordsFound(_ sentences: [String]) -> Int {
var maxWords = 0
if sentences.count == 0 { return maxWords }
for sentence in sentences {
let words = countWords(sentence)
if words > maxWords {
maxWords = words
}
}
return maxWords
}
func countWords(_ sentence:String) -> Int {
let aStr = Array(sentence)
var count = 0
for i in 0...aStr.count - 1 {
if aStr[i] == " " {
count += 1
}
}
count += 1
return count
}
let sentences = ["alice and bob love leetcode", "i think so too", "this is great thanks very much"]
print(mostWordsFound(sentences))
/*
1528. Shuffle String
You are given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.
Return the shuffled string.
Example 1:
Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3]
Output: "leetcode"
Explanation: As shown, "codeleet" becomes "leetcode" after shuffling.
*/
func restoreString(_ s: String, _ indices: [Int]) -> String {
var result = Array(repeating:"", count:s.count)
let aStr = Array(s)
for i in 0...indices.count - 1 {
result[indices[i]] = String(aStr[i])
}
return result.joined()
}
let s = "codeleet", indices = [4,5,6,7,0,2,1,3]
//let s = "abc", indices = [1,2,3]
print(restoreString(s, indices))
/*
2325. Decode the Message
You are given the strings key and message, which represent a cipher key and a secret message, respectively. The steps to decode message are as follows:
Use the first appearance of all 26 lowercase English letters in key as the order of the substitution table.
Align the substitution table with the regular English alphabet.
Each letter in message is then substituted using the table.
Spaces ' ' are transformed to themselves.
For example, given key = "happy boy" (actual key would have at least one instance of each letter in the alphabet), we have the partial substitution table of ('h' -> 'a', 'a' -> 'b', 'p' -> 'c', 'y' -> 'd', 'b' -> 'e', 'o' -> 'f').
Return the decoded message.
Example 1:
Input: key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv"
Output: "this is a secret"
Explanation: The diagram above shows the substitution table.
It is obtained by taking the first appearance of each letter in "the quick brown fox jumps over the lazy dog".
*/
func uniqueChar(_ key:String) -> [String] {
let aKey = Array(key)
var countChar = [Character:Int]()
var result = [String]()
for i in 0...aKey.count - 1 {
if countChar[aKey[i]] == nil && aKey[i] != " " {
countChar[aKey[i]] = 1
result.append(String(aKey[i]))
}
}
return result
}
func decodeMessage(_ key: String, _ message: String) -> String {
let key = uniqueChar(key)
let alphabet = Array("abcdefghijklmnopqrstuvwxyz")
let translate = Dictionary(uniqueKeysWithValues: zip(key,alphabet))
var result = ""
for c in message {
result += String(translate[String(c)] ?? " ")
}
return result
}
let key = "the quick brown fox jumps over the lazy dog"
let message = "vkbs bs t suepuv"
print(decodeMessage(key, message)) // This is a secret
/*1859. Sorting the Sentence
A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters.
A sentence can be shuffled by appending the 1-indexed word position to each word then rearranging the words in the sentence.
For example, the sentence "This is a sentence" can be shuffled as "sentence4 a3 is2 This1" or "is2 sentence4 This1 a3".
Given a shuffled sentence s containing no more than 9 words, reconstruct and return the original sentence.
Example 1:
Input: s = "is2 sentence4 This1 a3"
Output: "This is a sentence"
Explanation: Sort the words in s to their original positions "This1 is2 a3 sentence4", then remove the numbers.
*/
func sortSentence(_ s: String) -> String {
let aStr = Array(s)
var buffer = [Character]()
var result = [String]()
var pos:Character = " "
for i in 0...aStr.count - 1{
if aStr[i] == " " {
pos = buffer.removeLast()
if Int(String(pos))! > result.count - 1 {
for _ in 0...(Int(String(pos))! - result.count - 1) {
result.append("")
}
}
result[Int(String(pos))! - 1] = String(buffer)
buffer = [Character]()
} else {
buffer.append(aStr[i])
}
}
pos = buffer.removeLast()
result[Int(String(pos))! - 1] = String(buffer)
return result.joined(separator:" ")
}
let sentence = "is2 sentence4 This1 a3"
print(sortSentence(sentence))
let sentence2 = "Myself2 Me1 I4 and3"
print(sortSentence(sentence2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment