205. Isomorphic Strings(LeetCode)

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

For example,
Given "egg""add", return true.

Given "foo""bar", return false.

Given "paper""title", return true.

Note:
You may assume both s and t have the same length.

 1 class Solution {
 2 public:
 3     vector<vector<int>> fenbu(string s)
 4     {
 5         vector <vector<int>> vet;
 6         char c;
 7         for (int i = 0; i < s.size() - 1; i++)
 8         {
 9             vector<int> vet1;
10             if (s[i] != ‘0‘)
11             {
12                 vet1.push_back(i);
13                 c = s[i];
14                 s[i] = ‘0‘;
15                 for (int j = i + 1; j < s.size(); j++)
16                 {
17                     if (s[j] == c)
18                     {
19                         vet1.push_back(j);
20                         s[j] = ‘0‘;
21                     }
22                     else
23                     {
24                         if (j == s.size() - 1 && i == s.size() - 2)
25                         {
26                             vet.push_back(vet1);
27                             vet1.clear();
28                             vet1.push_back(j);
29                         }
30                     }
31                 }
32                 vet.push_back(vet1);
33
34             }
35         }
36         return vet;
37     }
38     bool isIsomorphic(string s, string t) {
39         if(s.size()==0&&t.size()==0)
40             return true;
41         vector<vector<int>> vet2;
42         vector<vector<int>> vet3;
43         vet2 = fenbu(s);
44         vet3 = fenbu(t);
45         if (vet3 == vet2)
46             return true;
47         else
48             return false;
49     }
50 };
时间: 2024-08-28 02:59:44

205. Isomorphic Strings(LeetCode)的相关文章

LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

翻译 给定两个字符串s和t,决定它们是否是同构的. 如果s中的元素被替换可以得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换另一个字符串的元素的过程中,所有字符的顺序必须保留. 没有两个字符可以被映射到相同的字符,但字符可以映射到该字符本身. 例如, 给定"egg","add",返回真. 给定"foo","bar",返回假. 给定"paper","title",返回真. 批

205. Isomorphic Strings - LeetCode

Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中相同位置字符的差是否相同 Java实现: public boolean isIsomorphic(String s, String t) { Map<String, Integer> map = new HashMap<>(); for (int i=0; i<s.length(

Windows Phone本地数据库(SQLCE):9、Connection Strings(翻译) (转)

这是“windows phone mango本地数据库(sqlce)”系列短片文章的第八篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知识点.我将谈谈在windows phone mango本地数据库中使用Connection Strings的问题. 1.ConnectionStrings是什么 在我们实际开始使用一个数据库之前,我们需要制定一个连接字符串,它告诉应用程序怎么连接数据库.一个连接字符串可以被用来做数据库的配置值.在连

119. Pascal&#39;s Triangle II(LeetCode)

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? class Solution { public: vector<int> getRow(int rowIndex) { vector<int&

(LeetCode)杨辉三角形Pascal&#39;s Triangle

题目如下: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 实现代码如下: public class Solution { public List<List<Integer>> generate(int numRows) { Lis

第15个算法-实现 Trie (前缀树)(LeetCode)

解法代码来源 :https://blog.csdn.net/whdAlive/article/details/81084793 算法来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert("apple"

不邻接植花(leetcode)

有 N 个花园,按从 1 到 N 标记.在每个花园中,你打算种下四种花之一. paths[i] = [x, y] 描述了花园 x 到花园 y 的双向路径. 另外,没有花园有 3 条以上的路径可以进入或者离开. 你需要为每个花园选择一种花,使得通过路径相连的任何两个花园中的花的种类互不相同. 以数组形式返回选择的方案作为答案 answer,其中 answer[i] 为在第 (i+1) 个花园中种植的花的种类.花的种类用  1, 2, 3, 4 表示.保证存在答案. 示例 1: 输入:N = 3,

leetcode_205题——Isomorphic Strings(用的map)

Isomorphic Strings Total Accepted: 5891 Total Submissions: 24698My Submissions Question Solution Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences

[leedcode 205] Isomorphic Strings

Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters.