https://leetcode.com/problems/valid-anagram/description
242. Valid Anagram
給你兩個字串,判斷這兩個字串是不是可以透過交換字元位置相等。
思路:
1.字串長度不同不可能相同返回false,統計兩個字元的字元數量,不相等返回 false。
Java Code:
----------------------------------------------
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] count = new int[26];
for (int i = 0; i < s.length(); i++) {
count[s.charAt(i) - 'a']++;
count[t.charAt(i) - 'a']--;
}
for (int cnt : count) {
if (cnt != 0) {
return false;
}
}
return true;
}
}
----------------------------------------------
--
https://i.imgur.com/Df746ya.jpg
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.100.73.13 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1702726130.A.50A.html