首页 > 其他分享 >[LeetCode] 2785. Sort Vowels in a String

[LeetCode] 2785. Sort Vowels in a String

时间:2023-11-14 10:59:40浏览次数:25  
标签:2785 Sort String vowels 字母 Vowels list letter 元音

Given a 0-indexed string s, permute s to get a new string t such that:
All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i].
The vowels must be sorted in the nondecreasing order of their ASCII values. More formally, for pairs of indices i, j with 0 <= i < j < s.length such that s[i] and s[j] are vowels, then t[i] must not have a higher ASCII value than t[j].
Return the resulting string.

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.

Example 1:
Input: s = "lEetcOde"
Output: "lEOtcede"
Explanation: 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.

Example 2:
Input: s = "lYmpH"
Output: "lYmpH"
Explanation: There are no vowels in s (all characters in s are consonants), so we return "lYmpH".

Constraints:
1 <= s.length <= 105
s consists only of letters of the English alphabet in uppercase and lowercase.

将字符串中的元音字母排序。

给你一个下标从 0 开始的字符串 s ,将 s 中的元素重新 排列 得到新的字符串 t ,它满足:
所有辅音字母都在原来的位置上。更正式的,如果满足 0 <= i < s.length 的下标 i 处的 s[i] 是个辅音字母,那么 t[i] = s[i] 。
元音字母都必须以他们的 ASCII 值按 非递减 顺序排列。更正式的,对于满足 0 <= i < j < s.length 的下标 i 和 j ,如果 s[i] 和 s[j] 都是元音字母,那么 t[i] 的 ASCII 值不能大于 t[j] 的 ASCII 值。
请你返回结果字母串。
元音字母为 'a' ,'e' ,'i' ,'o' 和 'u' ,它们可能是小写字母也可能是大写字母,辅音字母是除了这 5 个字母以外的所有字母。

思路

思路就是排序,但是这道题我们只对元音字母排序。具体做法我们需要遍历 input 字符串两次。第一次遍历,我们可以用一个 list 把所有的元音字母记下来,然后对这个 list 按字典序排序,这样 list 中字典序较大的字母在前。第二次遍历,如果遇到的 index 原本是一个辅音字母,则直接加入 stringbuilder,如果遇到的 index 原本是元音字母,则去 list 中拿一个字母出来放到这个位置上。

复杂度

时间O(nlogn)
空间O(n)

代码

class Solution {
    public String sortVowels(String s) {
        int n = s.length();
        // 把元音字母的index记录下来
        List<Character> list = new ArrayList<>();
        String str = "aeiouAEIOU";
        for (int i = 0; i < n; i++) {
            char letter = s.charAt(i);
            if (str.indexOf(letter) != -1) {
                list.add(letter);
            }
        }

        Collections.sort(list, (a, b) -> a.compareTo(b));
        // System.out.println(list);
        int j = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            char letter = s.charAt(i);
            if (str.indexOf(letter) == -1) {
                sb.append(letter);
            } else {
                sb.append(list.get(j++));
            }
        }
        return sb.toString();
    }
}

标签:2785,Sort,String,vowels,字母,Vowels,list,letter,元音
From: https://www.cnblogs.com/cnoodle/p/17831089.html

相关文章

  • 【刷题笔记】108. Convert Sorted Array to Binary Search Tree
    题目Givenanarraywhereelementsaresortedinascendingorder,convertittoaheightbalancedBST.Forthisproblem,aheight-balancedbinarytreeisdefinedasabinarytreeinwhichthedepthofthetwosubtreesof every nodeneverdifferbymorethan......
  • 冒泡排序(Bubble Sort)
    目录1.冒泡排序1.1基本原理1.2例子1.3示例代码2.魔炮排序2.1基本原理2.1例子2.2示例代码1.冒泡排序1.1基本原理冒泡排序(BubbleSort)是一种简单的排序算法。它重复地遍历待排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。遍历数列的工作是重复地进行直......
  • 数组的sort方法接受一个比较函数:compareFun(a, b); 如果返回的值>0,则调换a,b位置,即b的
    现有一组人员年龄的数据,要求将这些人员的年龄按照从小到大的顺序进行排列起来,要怎样来实现AfunctionnumberSort(a,b){returna-b;}vararr=newArray("23","6","12","35","76");document.write(arr.push(numberSort));BfunctionnumberSort(a,b){retu......
  • 前端学习-JavaScrip学习-sort()函数
    sort()函数默认按照字符串Unicode码排序如果希望按照数字大小排序,需要传参letarr=[2,4,5,6,22,9,10,111,2,1,32];console.log(arr.sort(function(a,b){returna-b;//升序//returnb-a;//降序}));参考链接:js排序——sort()排序用法......
  • qsort
    qsort快速排序和sizeof运算符一.qsort快速排序对于C语言中排序,冒泡排序是一种普遍常用的方法,但冒泡排序在绝大多数情况下只被用来整形排序,但在结构体等内容中我们常用到char,float,double等非整形数据,此时,qsort快速排序不失为一个很好的选择。1.头文件#include<stdlib.h>......
  • Set---SortedSet-NavigableSet-TreeSet
    SortedSet概述A{@linkSet}thatfurtherprovidesa<i>totalordering</i>onitselements.Theelementsareorderedusingtheir{@linkplainComparablenaturalordering},orbya{@linkComparator}typicallyprovidedatsortedsetcreationtime.......
  • Map---SortedMap&NavigableMap&TreeMap
    SortedMap概述A{@linkMap}thatfurtherprovidesa<em>totalordering</em>onitskeys.Themapisorderedaccordingtothe{@linkplainComparablenaturalordering}ofitskeys,orbya{@linkComparator}typicallyprovidedatsortedmapcreati......
  • Collectons.sort的坑
    [Requestprocessingfailed;nestedexceptionisjava.lang.IllegalArgumentException:Comparisonmethodviolatesitsgeneralcontract!]withrootcausejava.lang.IllegalArgumentException:Comparisonmethodviolatesitsgeneralcontract!atjava.util.Ti......
  • std::sort 传入成员函数指针报错的解决方案
    问题引入有一个类A,A的某个成员函数需要对A的某些变量进行std::sort,同时要调用A的另一个成员函数作为比较器。如代码所示:structA{vector<int>pos={0,4,2,5,3};boolcmp(intx,inty){returnpos[x]<pos[y];}voiddemo(){vector<int>a={2......
  • SortableJS:vuedraggable实现元素拖放排序
    文档:https://sortablejs.github.io/Sortable/github:https://github.com/SortableJS/SortableVue2:https://github.com/SortableJS/Vue.DraggableVue3:https://github.com/SortableJS/vue.draggable.nextnpmhttps://www.npmjs.com/package/vuedraggable#vue2npminst......