LeetCode contest 185 1417. 重新格式化字符串

简介: LeetCode contest 185 1417. 重新格式化字符串

LeetCode contest 185 1417. 重新格式化字符串


Table of Contents

一、中文版

二、英文版

三、My answer

四、解题报告


一、中文版

给你一个混合了数字和字母的字符串 s,其中的字母均为小写英文字母。

请你将该字符串重新格式化,使得任意两个相邻字符的类型都不同。也就是说,字母后面应该跟着数字,而数字后面应该跟着字母。

请你返回 重新格式化后 的字符串;如果无法按要求重新格式化,则返回一个 空字符串 。

示例 1:

输入:s = "a0b1c2"

输出:"0a1b2c"

解释:"0a1b2c" 中任意两个相邻字符的类型都不同。 "a0b1c2", "0a1b2c", "0c2a1b" 也是满足题目要求的答案。

示例 2:

输入:s = "leetcode"

输出:""

解释:"leetcode" 中只有字母,所以无法满足重新格式化的条件。

示例 3:

输入:s = "1229857369"

输出:""

解释:"1229857369" 中只有数字,所以无法满足重新格式化的条件。

示例 4:

输入:s = "covid2019"

输出:"c2o0v1i9d"

示例 5:

输入:s = "ab123"

输出:"1a2b3"

提示:

1 <= s.length <= 500

s 仅由小写英文字母和/或数字组成。

二、英文版

Given alphanumeric string s. (Alphanumeric string is a string consisting of lowercase English letters and digits).
You have to find a permutation of the string where no letter is followed by another letter and no digit is followed by another digit. That is, no two adjacent characters have the same type.
Return the reformatted string or return an empty string if it is impossible to reformat the string.
Example 1:
Input: s = "a0b1c2"
Output: "0a1b2c"
Explanation: "0a1b2c" doesn't have any letter followed by digit or any digit followed by character. "a0b1c2", "0a1b2c", "0c2a1b" are also valid permutations.
Example 2:
Input: s = "leetcode"
Output: ""
Explanation: "leetcode" has only characters so we cannot separate them by digits.
Example 3:
Input: s = "1229857369"
Output: ""
Explanation: "1229857369" has only digits so we cannot separate them by characters.
Example 4:
Input: s = "covid2019"
Output: "c2o0v1i9d"
Example 5:
Input: s = "ab123"
Output: "1a2b3"
Constraints:
1 <= s.length <= 500
s consists of only lowercase English letters and/or digits.

三、My answer

class Solution:
    def reformat(self, s: str) -> str:
        nums = []
        strings = []
        res = ''
        for i in s:
            if ord(i) >= 48 and ord(i) <= 57:
                nums.append(int(i))
            else:
                strings.append(i)
        if abs(len(nums)-len(strings)) > 1:
            return res
        elif len(nums) > len(strings):
            for i in range(len(strings)):
                res += str(nums[i])
                res += strings[i]
            res += str(nums[-1])
        elif len(nums) < len(strings):
            for i in range(len(nums)):
                res += strings[i]
                res += str(nums[i])
            res += strings[-1]
        else:
            for i in range(len(nums)):
                res += strings[i]
                res += str(nums[i])
        return res

四、解题报告

如果数字个数和字母个数相差大于1,那么一定不符合题意。

如果个数相差小于等于1,那么有三种情况:

1、数字个数 > 字母个数

2、字母个数 > 数字个数

3、个数相等

我们把个数多的放在开头,如果个数相同,谁先谁后无所谓。

以上,问题解决~

相关文章
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
|
存储 前端开发 算法
LeetCode重新格式化字符串使用JavaScript解题|前端学算法
LeetCode重新格式化字符串使用JavaScript解题|前端学算法
92 0
LeetCode重新格式化字符串使用JavaScript解题|前端学算法
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5475. 统计好三元组 Count Good Triplets
LeetCode contest 200 5475. 统计好三元组 Count Good Triplets
LeetCode contest 199 灯泡开关 IV Bulb Switcher IV
LeetCode contest 199 灯泡开关 IV Bulb Switcher IV
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
|
机器学习/深度学习
LeetCode contest 190 5416. 检查单词是否为句中其他单词的前缀
LeetCode contest 190 5416. 检查单词是否为句中其他单词的前缀
|
算法 C#
LeetCode contest 189 5413. 重新排列句子中的单词
LeetCode contest 189 5413. 重新排列句子中的单词
LeetCode contest 189 5412. 在既定时间做作业的学生人数
LeetCode contest 189 5412. 在既定时间做作业的学生人数