首页 > 其他分享 >leetcode-1417-easy

leetcode-1417-easy

时间:2022-11-04 19:12:49浏览次数:42  
标签:digits letters string pop return easy sb leetcode 1417

Reformat The String

You are given an 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: No two adjacent characters have the same type in "0a1b2c". "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.
Constraints:

1 <= s.length <= 500
s consists of only lowercase English letters and/or digits.

思路一:数字和字母分别存储,如果是合法的字符串,他们的差值 <= 1。拼接新字符串时,按间隔拼接即可

public String reformat(String s) {
    char[] chars = s.toCharArray();

    Deque<Character> digits = new ArrayDeque<>();
    Deque<Character> letters = new ArrayDeque<>();

    for (char c : chars) {
        if (isLetter(c)) letters.add(c);
        else digits.add(c);
    }

    if (Math.abs(digits.size() - letters.size()) > 1) return s;

    StringBuilder sb = new StringBuilder();
    int min = Math.min(digits.size(), letters.size());
    for (int i = 0; i < min; i++) {
        sb.append(digits.pop()).append(letters.pop());
    }

    if (!digits.isEmpty()) {
        sb.append(digits.pop());
    }

    if (!letters.isEmpty()) {
        sb.insert(0, letters.pop());
    }

    return sb.toString();
}

private static boolean isLetter(char c) {
    return c >= 'a' && c <= 'z';
}

思路二:看了一下题解,可以在原字符串上做双指针交换字符串,效率和空间会好很多

标签:digits,letters,string,pop,return,easy,sb,leetcode,1417
From: https://www.cnblogs.com/iyiluo/p/16858840.html

相关文章

  • leetcode-771-easy
    JewelsandStonesYou'regivenstringsjewelsrepresentingthetypesofstonesthatarejewels,andstonesrepresentingthestonesyouhave.Eachcharacterin......
  • leetcode-1200-easy
    MinimumAbsoluteDifferenceGivenanarrayofdistinctintegersarr,findallpairsofelementswiththeminimumabsolutedifferenceofanytwoelements.Retu......
  • leetcode-1984-easy
    MinimumDifferenceBetweenHighestandLowestofKScoresYouaregivena0-indexedintegerarraynums,wherenums[i]representsthescoreoftheithstudent.......
  • EasyCVR国标GB28181协议接入下的TCP和UDP模式说明及差异
    有用户在使用我们的平台时,经常会出现对于端口的疑问,同时也不了解端口的差别。今天我们来解释说明下EasyCVR平台关于国标GB28181协议接入下的TCP和UDP模式的说明及差异。......
  • 视频融合平台EasyCVR各项数据正常,却无法用海康NVR接入是什么原因?
    EasyCVR视频融合云平台开放度高、兼容性强、可支持灵活拓展与第三方集成,目前已经成为安防市场主流的视频能力层服务平台。平台可支持多协议、多类型的设备接入,包括国标GB28......
  • 我用EasyExcel优化了公司的导出(附踩坑记录)
    背景介绍最近要改一个导出的功能,在原有的基础上,在导出一份明细数据,要求导出内容加在原有excel的第二个sheet上。考虑到数据量还比较大,干脆引入阿里的EasyExcel来做......
  • leetcode 160. 相交链表 js 实现
    给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。图示两个链表在节点 c1 开始相交: ......
  • leetcode-136. 只出现一次的数字
    题目描述给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。说明说明:你的算法应该具有线性时间复杂度。你可以不......
  • LeetCode刷题记录.Day5
    反转链表题目链接206.反转链表-力扣(LeetCode)classSolution{public:ListNode*reverseList(ListNode*head){ListNode*temp;ListNode*c......
  • leetcode java 杨辉三角
    简介杨辉三角是一道简单题,可以通过类似一层推下一层的方式进行计算,但是好像看过一个题解,采用的方式是组合数。本来想采用组合数,尝试了double溢出尝试了long溢出,尝试......