LeetCode Weekly Contest 9 第九周周赛

422. Valid Word Square

Given a sequence of words, check whether it forms a valid word square.

A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < max(numRows, numColumns).
Note:
1. The number of words given is at least 1 and does not exceed 500.
2. Word length will be at least 1 and does not exceed 500.
3. Each word contains only lowercase English alphabet a-z.
大致就是讲给定一组string数组,判断string数组是不是符合要求。要求方块横着读和竖着读是一样的。
直接判断就行了。
golang AC代码:

func validWordSquare(words []string) bool {
    str := ""
    for i:=0 ;i< len(words);i++{
        str = ""
        for j:=0;j<len(words);j++{
            if i < len(words[j]){
                str += string(words[j][i])
            }else{
                break
            }
        }
        if str != words[i]{
            return false
        }

    }
    return true
}

423. Reconstruct Original Digits from English

Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order.

Note:
1. Input contains only lowercase English letters.
2. Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as “abc” or “zerone” are not permitted.
3. Input length is less than 50,000.

给一串乱序的字母序列,求这个字母序列重组之后所代表的数字串。
比如:

Input: "owoztneoer"

Output: "012"

题目中说给出的字母序列都是必定合法而且唯一的。

这题主要是根据0-9的英文中独占的字母来进行判断的。判断的顺序如下:

数字 英文 字母
0 zero z
6 six s
2 two w
7 seven s
4 four u
5 five v
1 one o
9 nine n
8 eight i
3 three t

从表的第一项开始,我们首先可以通过z的个数确定0的个数,然后通过s确定6的个数。依次类推。需要注意的是9的个数是n的个数的一半。最后就能得到乱序序列的原来的数字组合了。
golang AC代码:

func originalDigits(s string) string {
    nums := []string{"zero","one","two","three","four","five","six","seven","eight","nine"}
    var count [10]int
    var acount [26]int
    for i:=0;i<len(s);i++{
        acount[int(s[i]-'a')] ++
    }
    if acount[25]>0{
        str := nums[0]
        count[0] = acount[25]
        for i:=0;i<len(str);i++{
            acount[int(str[i]-'a')] -= count[0]
        }
    }
    if (acount[int('x'-'a')]>0){
        str := nums[6]
        count[6] = acount[int('x'-'a')]
        for i:=0;i<len(str);i++{
            acount[int(str[i]-'a')] -= count[6]
        }
    }
    if (acount[int('w'-'a')]>0){
        str := nums[2]
        count[2] = acount[int('w'-'a')]
        for i:=0;i<len(str);i++{
            acount[int(str[i]-'a')] -= count[2]
        }
    }

    if (acount[int('s'-'a')]>0){
        str := nums[7]
        count[7] = acount[int('s'-'a')]
        for i:=0;i<len(str);i++{
            acount[int(str[i]-'a')] -= count[7]
        }
    }
    if (acount[int('u'-'a')]>0){
        str := nums[4]
        count[4] = acount[int('u'-'a')]
        for i:=0;i<len(str);i++{
            acount[int(str[i]-'a')] -= count[4]
        }
    }
    if (acount[int('v'-'a')]>0){
        str := nums[5]
        count[5] = acount[int('v'-'a')]
        for i:=0;i<len(str);i++{
            acount[int(str[i]-'a')] -= count[5]
        }
    }
        if (acount[int('o'-'a')]>0){
        str := nums[1]
        count[1] = acount[int('o'-'a')]
        for i:=0;i<len(str);i++{
            acount[int(str[i]-'a')] -= count[1]
        }
    }   
    if (acount[int('n'-'a')]>0){
        str := nums[9]
        count[9] = acount[int('n'-'a')]/2
        for i:=0;i<len(str);i++{
            acount[int(str[i]-'a')] -= count[9]
        }
    }
    if (acount[int('i'-'a')]>0){
        str := nums[8]
        count[8] = acount[int('i'-'a')]
        for i:=0;i<len(str);i++{
            acount[int(str[i]-'a')] -= count[8]
        }
    }
    count[3] = acount[int('r'-'a')]
    str := ""
    for i:=0;i<=9;i++{
        for j:=0;jstring(i+'0')
        }
    }
    return str
}

424. Longest Repeating Character Replacement

Given a string that consists of only uppercase English letters, you can replace any letter in the string with another letter at most k times. Find the length of a longest substring containing all repeating letters you can get after performing the above operations.
给定一串仅由大写字母组成的字符串,和一个数字k。假定这个字符串最多有k个字母能被替换成其他字母。求这个替换的过程中,最长的连续重复序列的长度。
这道题可以用枚举法,先假设以求最长的连续的A,那么在整个序列中,把不是A的点都标出来。然后看看把这些点中的某K个换成A。求出这个换的过程中的最长长度。
算法复杂度是o(n).
AC golang代码如下:

func characterReplacement(s string, k int) int {
    dp := make([]int,len(s)+1)
    var ch [26]bool
    max := 0
    if k == 0{
        length := 1
        for i :=1;i<len(s);i++{
            if s[i-1] == s[i]{
                length ++
            }else{
                if max < length{
                    max = length
                }
                length = 1
            }

        }
        if max < length{
                    max = length
                }
        return max;
    }
    //length := 0
    for i :=0;i<len(s);i++{
        ch[s[i]-'A'] = true
    }
    for i :=0;i<26;i++{
        if ch[i] == false{
            continue
        }
        c := 'A' + i
    //  if c == 75{
    //      fmt.Println("break")
    //  }
        idx :=0
        for j:=0;j<len(s);j++{
            if int(s[j])!=c{
                dp[idx] = j
                idx++
            //else{
        //      fmt.Print(j," ")
            }
        }
        //fmt.Println("|",c)
        if idx <= k{
            return len(s)
        }
        if max < dp[k]{
            max = dp[k]
        }
        for j:= k+1;j if dp[j] - dp[j-k-1] - 1 >= max{
                max = dp[j] - dp[j-k-1] - 1
                //fmt.Println(c)
            }
        }
    }
    return max
}

425 Word Squares

没做出来,贴一个别人的python代码,用了trie树加dfs:


class Solution(object):
    def wordSquares(self, words):
        """
        :type words: List[str]
        :rtype: List[List[str]]
        """
        self.l = len(words[0])

        self.trie = self.build(words)

        self.res = []

        for word in words:
            self.dfs(words, self.trie, [word])

        return self.res

    def dfs(self, words, trie, lst):
        if len(lst) == self.l:
            self.res.append(lst)
            return

        prefix = ''
        for i in range(len(lst)):
            prefix += lst[i][len(lst)]

        for s in self.get(trie, prefix):
            self.dfs(words, trie, lst + [s])


    def build(self, words):
        trie = {}

        for word in words:
            t = trie

            for c in word:
                if c not in t:
                    t[c] = {}
                t = t[c]

            t['#'] = '#'

        return trie


    def get(self, trie, prefix):
        res = []

        t = trie
        for c in prefix:
            if c not in t:
                return res
            t = t[c]

        for s in self.getall(t):
            res.append(prefix + s)

        return res

    def getall(self, t):
        res = []
        if '#' in t: return ['']

        for c in t:
            if c != '#':
                for s in self.getall(t[c]):
                    res.append(c + s)
        return res



你可能感兴趣的:(leetcode)