给你一个字符串 s ,将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。
示例 1:
输入:s = "Hello"
输出:"hello"
var toLowerCase = function(s) {
return s.toLowerCase()
}; function toLowerCase(s: string): string {
return s.toLowerCase()
}; class Solution {
public String toLowerCase(String s) {
return s.toLowerCase();
}
} func toLowerCase(s string) string {
return strings.ToLower(s)
} char * toLowerCase(char * s){
int n = strlen(s);
for (int i = 0; i < n; i++) {
s[i] = tolower(s[i]);
}
return s;
} class Solution {
public:
string toLowerCase(string s) {
for (char& c : s) {
c = tolower(c);
}
return s;
}
}; public class Solution {
public string ToLowerCase(string s) {
return s.ToLower();
}
} class Solution:
def toLowerCase(self, s: str) -> str:
return s.lower() class Solution {
function toLowerCase($s) {
return strtolower($s);
}
} var toLowerCase = function(s) {
const sb = [], n = s.length
for (let i = 0; i < n; i++) {
if (s.charCodeAt(i) >= 65 && s.charCodeAt(i) <= 90) sb.push(s.charCodeAt(i) | 32)
else sb.push(s.charCodeAt(i))
}
return String.fromCharCode.apply(null, sb)
}; function toLowerCase(s: string): string {
const n = s.length, sb = new Uint8Array(n)
for (let i = 0; i < n; i++) {
let c = s.charCodeAt(i)
if (c >= 65 && c <= 90) c |= 32
sb[i] = c
}
return String.fromCharCode.apply(null, Array.prototype.slice.call(sb))
}; class Solution {
public String toLowerCase(String s) {
StringBuilder sb = new StringBuilder();
int n = s.length();
for (int i = 0; i < n; i++) {
char c = s.charAt(i);
if (c >= 65 && c <= 90) c |= 32;
sb.append(c);
}
return sb.toString();
}
} func toLowerCase(s string) string {
sb := strings.Builder{}
for _, c := range s {
if c >= 65 && c <= 90 {
c |= 32
}
sb.WriteRune(c)
}
return sb.String()
} char * toLowerCase(char * s){
int n = strlen(s);
for (int i = 0; i < n; i++) {
if (s[i] >= 65 && s[i] <= 90) s[i] |= 32;
}
return s;
} class Solution {
public:
string toLowerCase(string s) {
for (char& c : s) {
if (c >= 65 && c <= 90) c |= 32;
}
return s;
}
}; public class Solution {
public string ToLowerCase(string s) {
StringBuilder sb = new StringBuilder();
foreach(char c in s) {
if (c >= 65 && c <= 90) sb.Append((char)(c | 32));
else sb.Append(c);
}
return sb.ToString();
}
} class Solution:
def toLowerCase(self, s: str) -> str:
n = len(s)
r = ['a'] * n
for i, ch in enumerate(s):
c = ord(ch)
if c >= 65 and c <= 90: c |= 32
r[i] = chr(c)
return ''.join(r) class Solution {
function toLowerCase($s) {
$sb = [];
$n = strlen($s);
for ($i = 0; $i < $n; $i++) {
$c = ord($s[$i]);
if ($c >= 65 && $c <= 90) $c |= 32;
$sb []= chr($c);
}
return implode('', $sb);
}
} 给你一个字符串 s,它由数字('0' - '9')和 '- 'i')分别用('1' - '9')表示。
字符('j' - 'z')分别用('10#' - '26 返回映射之后形成的新字符串。
题目数据保证映射始终唯一。
示例 1:
输入:s = "10#11-> "10, "k" -> "11, "a" -> "1" , "b" -> "2".
var freqAlphabets = function(s) {
let r = ''
for (let i = 0, n = s.length; i < n; i++) {
if (i + 2 < n && s[i + 2] === '{
r += String.fromCharCode(+(s[i] + s[i + 1]) + 96)
i += 2
} else r += String.fromCharCode(+s[i] + 96)
}
return r
}; function freqAlphabets(s: string): string {
const n = s.length
let r = ''
for (let i = 0; i < n; i++) {
if (i + 2 < n && s[i + 2] === '{
r += String.fromCharCode(+(s[i] + s[i + 1]) + 96)
i += 2
} else {
r += String.fromCharCode(+s[i] + 96)
}
}
return r
}; func freqAlphabets(s string) string {
n, r := len(s), strings.Builder{}
for i := 0; i < n; i++ {
if i + 2 < n && s[i + 2] == '{
r.WriteByte((s[i] - '0') * 10 + s[i + 1] - '0' + 96)
i += 2
} else {
r.WriteByte(s[i] - '0' + 96)
}
}
return r.String()
} char * freqAlphabets(char * s){
int n = strlen(s), pos = 0;
char *r = (char *)malloc(sizeof(char) * (n + 1));
for (int i = 0; i < n; i++) {
if (i + 2 < n && s[i + 2] == '{
r[pos++] = (s[i] - '0') * 10 + (s[i + 1] - '0') + 96;
i += 2;
} else {
r[pos++] = (s[i] - '0') + 96;
}
}
r[pos] = '\0';
return r;
} class Solution {
public:
string freqAlphabets(string s) {
int n = s.size();
string r = "";
for (int i = 0; i < n; i++) {
if (i + 2 < n && s[i + 2] == '{
r += (s[i] - '0') * 10 + (s[i + 1] - '0') + 96;
i += 2;
} else {
r += (s[i] - '0') + 96;
}
}
return r;
}
}; public class Solution {
public string FreqAlphabets(string s) {
int n = s.Length;
StringBuilder r = new StringBuilder();
for (int i = 0; i < n; i++) {
if (i + 2 < n && s[i + 2] == '{
r.Append((char)((s[i] - '0') * 10 + (s[i + 1] - '0') + 96));
i += 2;
} else {
r.Append((char)((s[i] - '0') + 96));
}
}
return r.ToString();
} class Solution {
public String freqAlphabets(String s) {
int n = s.length();
StringBuilder r = new StringBuilder();
for (int i = 0; i < n; i++) {
if (i + 2 < n && s.charAt(i + 2) == '{
r.append((char)((s.charAt(i) - '0') * 10 + s.charAt(i + 1) - '0' + 96));
i += 2;
} else {
r.append((char)(s.charAt(i) - '0' + 96));
}
}
return r.toString();
}
} class Solution {
function freqAlphabets($s) {
$n = strlen($s);
$r = '';
for ($i = 0; $i < $n; $i++) {
if ($i + 2 < $n && $s[$i + 2] === '{
$r .= chr(($s[$i] - '0') * 10 + $s[$i + 1] - '0' + 96);
$i += 2;
} else {
$r .= chr($s[$i] - '0' + 96);
}
}
return $r;
}
} class Solution:
def freqAlphabets(self, s: str) -> str:
n, r, i = len(s), '', 0
while i < n:
if i + 2 < n and s[i + 2] == '
r += chr(int(s[i] + s[i + 1]) + 96)
i += 3
else:
r += chr(int(s[i]) + 96)
i += 1
return r 某种外星语也使用英文小写字母,但可能顺序 order 不同。字母表的顺序(order)是一些小写字母的排列。
给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在这种外星语中按字典序排列时,返回 true;否则,返回 false。
示例 1:
输入:words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
输出:true
解释:在该语言的字母表中,'h' 位于 'l' 之前,所以单词序列是按字典序排列的。
var isAlienSorted = function(words, order) {
const h = new Uint8Array(26)
for (let i = 0; i < order.length; i++) {
h[order.charCodeAt(i) - 97] = i
}
const compare = (a, b, i) => {
if (i === a.length || i === b.length) return a.length <= b.length
const x = h[a.charCodeAt(i) - 97], y = h[b.charCodeAt(i) - 97]
return x === y ? compare(a, b, i + 1) : x < y
}
for (let i = 1; i < words.length; i++) {
if (compare(words[i - 1], words[i], 0) === false) return false
}
return true
}; function isAlienSorted(words: string[], order: string): boolean {
const h = new Uint8Array(26)
for (let i = 0; i < 26; i++) h[order.charCodeAt(i) - 97] = i
const compare = (a: string, b: string): boolean => {
for (let i = 0; i < Math.min(a.length, b.length); i++) {
const x = h[a.charCodeAt(i) - 97], y = h[b.charCodeAt(i) - 97]
if (x !== y) return x < y
}
return a.length <= b.length
}
for (let i = 1; i < words.length; i++) {
if (compare(words[i - 1], words[i]) === false) return false
}
return true
}; func isAlienSorted(words []string, order string) bool {
h := [26]int{}
for i, charCode := range order {
h[charCode - 97] = i
}
compare := func(a string, b string) bool {
for i := 0; i < len(a) && i < len(b); i++ {
x, y := h[a[i] - 97], h[b[i] - 97]
if x != y {
return x < y
}
}
return len(a) <= len(b)
}
for i, n := 1, len(words); i < n; i++ {
if compare(words[i - 1], words[i]) == false {
return false
}
}
return true
} class Solution {
private $h = [];
function isAlienSorted($words, $order) {
for ($i = 0; $i < 26; $i++) {
$this->h[ord($order[$i]) - 97] = $i;
}
for ($i = 1; $i < count($words); $i++) {
if ($this->compare($words[$i - 1], $words[$i]) === false) return false;
}
return true;
}
function compare(&$a, &$b) {
for ($i = 0; $i < min(strlen($a), strlen($b)); $i++) {
$x = $this->h[ord($a[$i]) - 97];
$y = $this->h[ord($b[$i]) - 97];
if ($x !== $y) return $x < $y;
}
return strlen($a) <= strlen($b);
}
} class Solution {
private int[] h = new int[26];
public boolean isAlienSorted(String[] words, String order) {
for (int i = 0; i < 26; i++) h[order.charAt(i) - 97] = i;
for (int i = 1; i < words.length; i++) {
if (compare(words[i - 1], words[i]) == false) return false;
}
return true;
}
public boolean compare(String a, String b) {
for (int i = 0; i < Math.min(a.length(), b.length()); i++) {
int x = h[a.charAt(i) - 97];
int y = h[b.charAt(i) - 97];
if (x != y) return x < y;
}
return a.length() <= b.length();
}
} class Solution:
def isAlienSorted(self, words: List[str], order: str) -> bool:
h = [0] * 26
for i, char in enumerate(order):
h[ord(char) - 97] = i
def compare(a: str, b: str) -> bool:
for i in range(min(len(a), len(b))):
x, y = h[ord(a[i]) - 97], h[ord(b[i]) - 97]
if x != y: return x < y
return len(a) <= len(b)
for i in range(1, len(words)):
if not compare(words[i - 1], words[i]): return False
return True bool compare(int * h, char * a, char * b) {
int na = strlen(a), nb = strlen(b);
for (int i = 0; i < na && i < nb; i++) {
int x = h[a[i] - 97], y = h[b[i] - 97];
if (x != y) {
return x < y;
}
}
return na <= nb;
}
bool isAlienSorted(char ** words, int wordsSize, char * order){
int n = strlen(order);
int* h = (int *)malloc(sizeof(int) * 26);
for (int i = 0; i < n; i++) {
h[order[i] - 97] = i;
}
for (int i = 1; i < wordsSize; i++) {
if (compare(h, words[i - 1], words[i]) == false) return false;
}
return true;
} class Solution {
public:
int h[26];
bool isAlienSorted(vector<string>& words, string order) {
for (int i = 0; i < order.size(); i++) {
h[order[i] - 97] = i;
}
for (int i = 1; i < words.size(); i++) {
if (compare(words[i - 1], words[i]) == false) return false;
}
return true;
}
bool compare(string a, string b) {
int na = a.size(), nb = b.size();
for (int i = 0; i < min(na, nb); i++) {
int x = h[a[i] - 97], y = h[b[i] - 97];
if (x != y) return x < y;
}
return na <= nb;
}
}; public class Solution {
private int[] h = new int[26];
public bool IsAlienSorted(string[] words, string order) {
for (int i = 0; i < order.Length; i++) {
h[order[i] - 97] = i;
}
for (int i = 1; i < words.Length; i++) {
if (compare(words[i - 1], words[i]) == false) return false;
}
return true;
}
public bool compare(string a, string b) {
int na = a.Length, nb = b.Length;
for (int i = 0; i < Math.Min(na, nb); i++) {
int x = h[a[i] - 97], y = h[b[i] - 97];
if (x != y) return x < y;
}
return na <= nb;
}
}