返回介绍

solution / 1000-1099 / 1061.Lexicographically Smallest Equivalent String / README_EN

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

1061. Lexicographically Smallest Equivalent String

中文文档

Description

You are given two strings of the same length s1 and s2 and a string baseStr.

We say s1[i] and s2[i] are equivalent characters.

  • For example, if s1 = "abc" and s2 = "cde", then we have 'a' == 'c', 'b' == 'd', and 'c' == 'e'.

Equivalent characters follow the usual rules of any equivalence relation:

  • Reflexivity: 'a' == 'a'.
  • Symmetry: 'a' == 'b' implies 'b' == 'a'.
  • Transitivity: 'a' == 'b' and 'b' == 'c' implies 'a' == 'c'.

For example, given the equivalency information from s1 = "abc" and s2 = "cde", "acd" and "aab" are equivalent strings of baseStr = "eed", and "aab" is the lexicographically smallest equivalent string of baseStr.

Return _the lexicographically smallest equivalent string of _baseStr_ by using the equivalency information from _s1_ and _s2.

 

Example 1:

Input: s1 = "parker", s2 = "morris", baseStr = "parser"
Output: "makkek"
Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".

Example 2:

Input: s1 = "hello", s2 = "world", baseStr = "hold"
Output: "hdld"
Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".

Example 3:

Input: s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
Output: "aauaaaaada"
Explanation: We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".

 

Constraints:

  • 1 <= s1.length, s2.length, baseStr <= 1000
  • s1.length == s2.length
  • s1, s2, and baseStr consist of lowercase English letters.

Solutions

Solution 1

class Solution:
  def smallestEquivalentString(self, s1: str, s2: str, baseStr: str) -> str:
    p = list(range(26))

    def find(x):
      if p[x] != x:
        p[x] = find(p[x])
      return p[x]

    for i in range(len(s1)):
      a, b = ord(s1[i]) - ord('a'), ord(s2[i]) - ord('a')
      pa, pb = find(a), find(b)
      if pa < pb:
        p[pb] = pa
      else:
        p[pa] = pb

    res = []
    for a in baseStr:
      a = ord(a) - ord('a')
      res.append(chr(find(a) + ord('a')))
    return ''.join(res)
class Solution {
  private int[] p;

  public String smallestEquivalentString(String s1, String s2, String baseStr) {
    p = new int[26];
    for (int i = 0; i < 26; ++i) {
      p[i] = i;
    }
    for (int i = 0; i < s1.length(); ++i) {
      int a = s1.charAt(i) - 'a', b = s2.charAt(i) - 'a';
      int pa = find(a), pb = find(b);
      if (pa < pb) {
        p[pb] = pa;
      } else {
        p[pa] = pb;
      }
    }
    StringBuilder sb = new StringBuilder();
    for (char a : baseStr.toCharArray()) {
      char b = (char) (find(a - 'a') + 'a');
      sb.append(b);
    }
    return sb.toString();
  }

  private int find(int x) {
    if (p[x] != x) {
      p[x] = find(p[x]);
    }
    return p[x];
  }
}
class Solution {
public:
  vector<int> p;

  string smallestEquivalentString(string s1, string s2, string baseStr) {
    p.resize(26);
    for (int i = 0; i < 26; ++i)
      p[i] = i;
    for (int i = 0; i < s1.size(); ++i) {
      int a = s1[i] - 'a', b = s2[i] - 'a';
      int pa = find(a), pb = find(b);
      if (pa < pb)
        p[pb] = pa;
      else
        p[pa] = pb;
    }
    string res = "";
    for (char a : baseStr) {
      char b = (char) (find(a - 'a') + 'a');
      res += b;
    }
    return res;
  }

  int find(int x) {
    if (p[x] != x)
      p[x] = find(p[x]);
    return p[x];
  }
};
var p []int

func smallestEquivalentString(s1 string, s2 string, baseStr string) string {
  p = make([]int, 26)
  for i := 0; i < 26; i++ {
    p[i] = i
  }
  for i := 0; i < len(s1); i++ {
    a, b := int(s1[i]-'a'), int(s2[i]-'a')
    pa, pb := find(a), find(b)
    if pa < pb {
      p[pb] = pa
    } else {
      p[pa] = pb
    }
  }
  var res []byte
  for _, a := range baseStr {
    b := byte(find(int(a-'a'))) + 'a'
    res = append(res, b)
  }
  return string(res)
}

func find(x int) int {
  if p[x] != x {
    p[x] = find(p[x])
  }
  return p[x]
}

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

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

发布评论

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