返回介绍

solution / 0700-0799 / 0784.Letter Case Permutation / README_EN

发布于 2024-06-17 01:03:34 字数 6244 浏览 0 评论 0 收藏 0

784. Letter Case Permutation

中文文档

Description

Given a string s, you can transform every letter individually to be lowercase or uppercase to create another string.

Return _a list of all possible strings we could create_. Return the output in any order.

 

Example 1:

Input: s = "a1b2"
Output: ["a1b2","a1B2","A1b2","A1B2"]

Example 2:

Input: s = "3z4"
Output: ["3z4","3Z4"]

 

Constraints:

  • 1 <= s.length <= 12
  • s consists of lowercase English letters, uppercase English letters, and digits.

Solutions

Solution 1

class Solution:
  def letterCasePermutation(self, s: str) -> List[str]:
    def dfs(i):
      if i >= len(s):
        ans.append(''.join(t))
        return
      dfs(i + 1)
      if t[i].isalpha():
        t[i] = chr(ord(t[i]) ^ 32)
        dfs(i + 1)

    t = list(s)
    ans = []
    dfs(0)
    return ans
class Solution {
  private List<String> ans = new ArrayList<>();
  private char[] t;

  public List<String> letterCasePermutation(String s) {
    t = s.toCharArray();
    dfs(0);
    return ans;
  }

  private void dfs(int i) {
    if (i >= t.length) {
      ans.add(String.valueOf(t));
      return;
    }
    dfs(i + 1);
    if (t[i] >= 'A') {
      t[i] ^= 32;
      dfs(i + 1);
    }
  }
}
class Solution {
public:
  vector<string> letterCasePermutation(string s) {
    vector<string> ans;
    function<void(int)> dfs = [&](int i) {
      if (i >= s.size()) {
        ans.emplace_back(s);
        return;
      }
      dfs(i + 1);
      if (s[i] >= 'A') {
        s[i] ^= 32;
        dfs(i + 1);
      }
    };
    dfs(0);
    return ans;
  }
};
func letterCasePermutation(s string) (ans []string) {
  t := []byte(s)
  var dfs func(int)
  dfs = func(i int) {
    if i >= len(t) {
      ans = append(ans, string(t))
      return
    }
    dfs(i + 1)
    if t[i] >= 'A' {
      t[i] ^= 32
      dfs(i + 1)
    }
  }

  dfs(0)
  return ans
}
function letterCasePermutation(s: string): string[] {
  const n = s.length;
  const cs = [...s];
  const res = [];
  const dfs = (i: number) => {
    if (i === n) {
      res.push(cs.join(''));
      return;
    }
    dfs(i + 1);
    if (cs[i] >= 'A') {
      cs[i] = String.fromCharCode(cs[i].charCodeAt(0) ^ 32);
      dfs(i + 1);
    }
  };
  dfs(0);
  return res;
}
impl Solution {
  fn dfs(i: usize, cs: &mut Vec<char>, res: &mut Vec<String>) {
    if i == cs.len() {
      res.push(cs.iter().collect());
      return;
    }
    Self::dfs(i + 1, cs, res);
    if cs[i] >= 'A' {
      cs[i] = char::from((cs[i] as u8) ^ 32);
      Self::dfs(i + 1, cs, res);
    }
  }

  pub fn letter_case_permutation(s: String) -> Vec<String> {
    let mut res = Vec::new();
    let mut cs = s.chars().collect::<Vec<char>>();
    Self::dfs(0, &mut cs, &mut res);
    res
  }
}

Solution 2

class Solution:
  def letterCasePermutation(self, s: str) -> List[str]:
    ans = []
    n = sum(c.isalpha() for c in s)
    for i in range(1 << n):
      j, t = 0, []
      for c in s:
        if c.isalpha():
          c = c.lower() if (i >> j) & 1 else c.upper()
          j += 1
        t.append(c)
      ans.append(''.join(t))
    return ans
class Solution {
  public List<String> letterCasePermutation(String s) {
    int n = 0;
    for (int i = 0; i < s.length(); ++i) {
      if (s.charAt(i) >= 'A') {
        ++n;
      }
    }
    List<String> ans = new ArrayList<>();
    for (int i = 0; i < 1 << n; ++i) {
      int j = 0;
      StringBuilder t = new StringBuilder();
      for (int k = 0; k < s.length(); ++k) {
        char x = s.charAt(k);
        if (x >= 'A') {
          x = ((i >> j) & 1) == 1 ? Character.toLowerCase(x) : Character.toUpperCase(x);
          ++j;
        }
        t.append(x);
      }
      ans.add(t.toString());
    }
    return ans;
  }
}
class Solution {
public:
  vector<string> letterCasePermutation(string s) {
    int n = 0;
    for (char c : s)
      if (isalpha(c)) ++n;
    vector<string> ans;
    for (int i = 0; i < 1 << n; ++i) {
      int j = 0;
      string t;
      for (char c : s) {
        if (isalpha(c)) {
          c = (i >> j & 1) ? tolower(c) : toupper(c);
          ++j;
        }
        t += c;
      }
      ans.emplace_back(t);
    }
    return ans;
  }
};
func letterCasePermutation(s string) (ans []string) {
  n := 0
  for _, c := range s {
    if c >= 'A' {
      n++
    }
  }
  for i := 0; i < 1<<n; i++ {
    j := 0
    t := []rune{}
    for _, c := range s {
      if c >= 'A' {
        if ((i >> j) & 1) == 1 {
          c = unicode.ToLower(c)
        } else {
          c = unicode.ToUpper(c)
        }
        j++
      }
      t = append(t, c)
    }
    ans = append(ans, string(t))
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文