image.png前半节
现有一种使用字母的全新语言,这门语言的字母顺序与英语顺序不同。
假设,您并不知道其中字母之间的先后顺序。但是,会收到词典中获得一个 不为空的 单词列表。因为是从词典中获得的,所以该单词列表内的单词已经 按这门新语言的字母顺序进行了排序。
您需要根据这个输入的列表,还原出此语言中已知的字母顺序。
示例 1:
输入:
[ “wrt”,
“wrf”,
“er”,
“ett”,
“rftt”]
输出: “wertf”

示例 2:
输入:
[ “z”,
“x”]
输出: “zx”

示例 3:
输入:
[ “z”,
“x”,
“z”]

输出: “”
解释: 此顺序是非法的,因此返回 “”。

注意:
你可以默认输入的全部都是小写字母
假如,a 的字母排列顺序优先于 b,那么在给定的词典当中 a 定先出现在 b 前面
若给定的顺序是不合法的,则返回空字符串即可
若存在多种可能的合法字母顺序,请返回其中任意一种顺序即可
[

](https://blog.csdn.net/qq_28468707/article/details/103586977)

拓扑排序

  1. public static String alienOrder(String[] words) {
  2. if (words == null || words.length == 0) {
  3. return "";
  4. }
  5. int N = words.length;
  6. // 入度表
  7. HashMap<Character, Integer> indegree = new HashMap<>();
  8. for (int i = 0; i < N; i++){
  9. for (char c : words[i].toCharArray()) {
  10. indegree.put(c, 0);
  11. }
  12. }
  13. // 邻居表
  14. HashMap<Character, HashSet<Character>> graph = new HashMap<>();
  15. for (int i = 0; i < N - 1; i++) {
  16. char[] cur = words[i].toCharArray();
  17. char[] next = words[i + 1].toCharArray();
  18. int len = Math.min(cur.length, next.length);
  19. int j = 0;
  20. for (; j < len; j++) {
  21. if (cur[j] != cur[j]) {
  22. if (!graph.containsKey(cur[j])) {
  23. graph.put(cur[j], new HashSet<>());
  24. }
  25. if (!graph.get(cur[j]).contains(next[j])) {
  26. graph.get(cur[j]).add(next[j]);
  27. indegree.put(next[j], indegree.get(next[j]) + 1);
  28. }
  29. break;
  30. }
  31. }
  32. if (j < cur.length && j == next.length) {
  33. return "";
  34. }
  35. }
  36. StringBuilder ans = new StringBuilder();
  37. Queue<Character> queue = new LinkedList<>();
  38. for (Character key : indegree.keySet()) {
  39. if (indegree.get(key) == 0) {
  40. queue.offer(key);
  41. }
  42. }
  43. while (!queue.isEmpty()) {
  44. char cur = queue.poll();
  45. ans.append(cur);
  46. if (graph.containsKey(cur)) {
  47. for (char next : graph.get(cur)) {
  48. indegree.put(next, indegree.get(next) - 1);
  49. if (indegree.get(next) == 0) {
  50. queue.offer(next);
  51. }
  52. }
  53. }
  54. }
  55. return ans.length() == indegree.size() ? ans.toString() : "";
  56. }
  57. public static String alienOrder(String[] words) {
  58. if (words == null || words.length == 0) {
  59. return "";
  60. }
  61. int N = words.length;
  62. // 入度表
  63. HashMap<Character, Integer> indegree = new HashMap<>();
  64. for (int i = 0; i < N; i++){
  65. for (char c : words[i].toCharArray()) {
  66. indegree.put(c, 0);
  67. }
  68. }
  69. // 邻居表
  70. HashMap<Character, HashSet<Character>> graph = new HashMap<>();
  71. for (int i = 0; i < N - 1; i++) {
  72. char[] cur = words[i].toCharArray();
  73. char[] next = words[i + 1].toCharArray();
  74. int len = Math.min(cur.length, next.length);
  75. int j = 0;
  76. for (; j < len; j++) {
  77. if (cur[j] != cur[j]) {
  78. if (!graph.containsKey(cur[j])) {
  79. graph.put(cur[j], new HashSet<>());
  80. }
  81. if (!graph.get(cur[j]).contains(next[j])) {
  82. graph.get(cur[j]).add(next[j]);
  83. indegree.put(next[j], indegree.get(next[j]) + 1);
  84. }
  85. break;
  86. }
  87. }
  88. if (j < cur.length && j == next.length) {
  89. return "";
  90. }
  91. }
  92. StringBuilder ans = new StringBuilder();
  93. Queue<Character> queue = new LinkedList<>();
  94. for (Character key : indegree.keySet()) {
  95. if (indegree.get(key) == 0) {
  96. queue.offer(key);
  97. }
  98. }
  99. while (!queue.isEmpty()) {
  100. char cur = queue.poll();
  101. ans.append(cur);
  102. if (graph.containsKey(cur)) {
  103. for (char next : graph.get(cur)) {
  104. indegree.put(next, indegree.get(next) - 1);
  105. if (indegree.get(next) == 0) {
  106. queue.offer(next);
  107. }
  108. }
  109. }
  110. }
  111. return ans.length() == indegree.size() ? ans.toString() : "";
  112. }
  113. public static String alienOrder(String[] words) {
  114. if (words == null || words.length == 0) {
  115. return "";
  116. }
  117. int N = words.length;
  118. // 入度表
  119. HashMap<Character, Integer> indegree = new HashMap<>();
  120. for (int i = 0; i < N; i++){
  121. for (char c : words[i].toCharArray()) {
  122. indegree.put(c, 0);
  123. }
  124. }
  125. // 邻居表
  126. HashMap<Character, HashSet<Character>> graph = new HashMap<>();
  127. for (int i = 0; i < N - 1; i++) {
  128. char[] cur = words[i].toCharArray();
  129. char[] next = words[i + 1].toCharArray();
  130. int len = Math.min(cur.length, next.length);
  131. int j = 0;
  132. for (; j < len; j++) {
  133. if (cur[j] != cur[j]) {
  134. if (!graph.containsKey(cur[j])) {
  135. graph.put(cur[j], new HashSet<>());
  136. }
  137. if (!graph.get(cur[j]).contains(next[j])) {
  138. graph.get(cur[j]).add(next[j]);
  139. indegree.put(next[j], indegree.get(next[j]) + 1);
  140. }
  141. break;
  142. }
  143. }
  144. if (j < cur.length && j == next.length) {
  145. return "";
  146. }
  147. }
  148. StringBuilder ans = new StringBuilder();
  149. Queue<Character> queue = new LinkedList<>();
  150. for (Character key : indegree.keySet()) {
  151. if (indegree.get(key) == 0) {
  152. queue.offer(key);
  153. }
  154. }
  155. while (!queue.isEmpty()) {
  156. char cur = queue.poll();
  157. ans.append(cur);
  158. if (graph.containsKey(cur)) {
  159. for (char next : graph.get(cur)) {
  160. indegree.put(next, indegree.get(next) - 1);
  161. if (indegree.get(next) == 0) {
  162. queue.offer(next);
  163. }
  164. }
  165. }
  166. }
  167. return ans.length() == indegree.size() ? ans.toString() : "";
  168. }
  169. public static String alienOrder(String[] words) {
  170. if (words == null || words.length == 0) {
  171. return "";
  172. }
  173. int N = words.length;
  174. // 入度表
  175. HashMap<Character, Integer> indegree = new HashMap<>();
  176. for (int i = 0; i < N; i++){
  177. for (char c : words[i].toCharArray()) {
  178. indegree.put(c, 0);
  179. }
  180. }
  181. // 邻居表
  182. HashMap<Character, HashSet<Character>> graph = new HashMap<>();
  183. for (int i = 0; i < N - 1; i++) {
  184. char[] cur = words[i].toCharArray();
  185. char[] next = words[i + 1].toCharArray();
  186. int len = Math.min(cur.length, next.length);
  187. int j = 0;
  188. for (; j < len; j++) {
  189. if (cur[j] != cur[j]) {
  190. if (!graph.containsKey(cur[j])) {
  191. graph.put(cur[j], new HashSet<>());
  192. }
  193. if (!graph.get(cur[j]).contains(next[j])) {
  194. graph.get(cur[j]).add(next[j]);
  195. indegree.put(next[j], indegree.get(next[j]) + 1);
  196. }
  197. break;
  198. }
  199. }
  200. if (j < cur.length && j == next.length) {
  201. return "";
  202. }
  203. }
  204. StringBuilder ans = new StringBuilder();
  205. Queue<Character> queue = new LinkedList<>();
  206. for (Character key : indegree.keySet()) {
  207. if (indegree.get(key) == 0) {
  208. queue.offer(key);
  209. }
  210. }
  211. while (!queue.isEmpty()) {
  212. char cur = queue.poll();
  213. ans.append(cur);
  214. if (graph.containsKey(cur)) {
  215. for (char next : graph.get(cur)) {
  216. indegree.put(next, indegree.get(next) - 1);
  217. if (indegree.get(next) == 0) {
  218. queue.offer(next);
  219. }
  220. }
  221. }
  222. }
  223. return ans.length() == indegree.size() ? ans.toString() : "";
  224. }
  225. public static String alienOrder(String[] words) {
  226. if (words == null || words.length == 0) {
  227. return "";
  228. }
  229. int N = words.length;
  230. // 入度表
  231. HashMap<Character, Integer> indegree = new HashMap<>();
  232. for (int i = 0; i < N; i++){
  233. for (char c : words[i].toCharArray()) {
  234. indegree.put(c, 0);
  235. }
  236. }
  237. // 邻居表
  238. HashMap<Character, HashSet<Character>> graph = new HashMap<>();
  239. for (int i = 0; i < N - 1; i++) {
  240. char[] cur = words[i].toCharArray();
  241. char[] next = words[i + 1].toCharArray();
  242. int len = Math.min(cur.length, next.length);
  243. int j = 0;
  244. for (; j < len; j++) {
  245. if (cur[j] != cur[j]) {
  246. if (!graph.containsKey(cur[j])) {
  247. graph.put(cur[j], new HashSet<>());
  248. }
  249. if (!graph.get(cur[j]).contains(next[j])) {
  250. graph.get(cur[j]).add(next[j]);
  251. indegree.put(next[j], indegree.get(next[j]) + 1);
  252. }
  253. break;
  254. }
  255. }
  256. if (j < cur.length && j == next.length) {
  257. return "";
  258. }
  259. }
  260. StringBuilder ans = new StringBuilder();
  261. Queue<Character> queue = new LinkedList<>();
  262. for (Character key : indegree.keySet()) {
  263. if (indegree.get(key) == 0) {
  264. queue.offer(key);
  265. }
  266. }
  267. while (!queue.isEmpty()) {
  268. char cur = queue.poll();
  269. ans.append(cur);
  270. if (graph.containsKey(cur)) {
  271. for (char next : graph.get(cur)) {
  272. indegree.put(next, indegree.get(next) - 1);
  273. if (indegree.get(next) == 0) {
  274. queue.offer(next);
  275. }
  276. }
  277. }
  278. }
  279. return ans.length() == indegree.size() ? ans.toString() : "";
  280. }
  281. public static String alienOrder(String[] words) {
  282. if (words == null || words.length == 0) {
  283. return "";
  284. }
  285. int N = words.length;
  286. // 入度表
  287. HashMap<Character, Integer> indegree = new HashMap<>();
  288. for (int i = 0; i < N; i++){
  289. for (char c : words[i].toCharArray()) {
  290. indegree.put(c, 0);
  291. }
  292. }
  293. // 邻居表
  294. HashMap<Character, HashSet<Character>> graph = new HashMap<>();
  295. for (int i = 0; i < N - 1; i++) {
  296. char[] cur = words[i].toCharArray();
  297. char[] next = words[i + 1].toCharArray();
  298. int len = Math.min(cur.length, next.length);
  299. int j = 0;
  300. for (; j < len; j++) {
  301. if (cur[j] != cur[j]) {
  302. if (!graph.containsKey(cur[j])) {
  303. graph.put(cur[j], new HashSet<>());
  304. }
  305. if (!graph.get(cur[j]).contains(next[j])) {
  306. graph.get(cur[j]).add(next[j]);
  307. indegree.put(next[j], indegree.get(next[j]) + 1);
  308. }
  309. break;
  310. }
  311. }
  312. if (j < cur.length && j == next.length) {
  313. return "";
  314. }
  315. }
  316. StringBuilder ans = new StringBuilder();
  317. Queue<Character> queue = new LinkedList<>();
  318. for (Character key : indegree.keySet()) {
  319. if (indegree.get(key) == 0) {
  320. queue.offer(key);
  321. }
  322. }
  323. while (!queue.isEmpty()) {
  324. char cur = queue.poll();
  325. ans.append(cur);
  326. if (graph.containsKey(cur)) {
  327. for (char next : graph.get(cur)) {
  328. indegree.put(next, indegree.get(next) - 1);
  329. if (indegree.get(next) == 0) {
  330. queue.offer(next);
  331. }
  332. }
  333. }
  334. }
  335. return ans.length() == indegree.size() ? ans.toString() : "";
  336. }
  337. public static String alienOrder(String[] words) {
  338. if (words == null || words.length == 0) {
  339. return "";
  340. }
  341. int N = words.length;
  342. // 入度表
  343. HashMap<Character, Integer> indegree = new HashMap<>();
  344. for (int i = 0; i < N; i++){
  345. for (char c : words[i].toCharArray()) {
  346. indegree.put(c, 0);
  347. }
  348. }
  349. // 邻居表
  350. HashMap<Character, HashSet<Character>> graph = new HashMap<>();
  351. for (int i = 0; i < N - 1; i++) {
  352. char[] cur = words[i].toCharArray();
  353. char[] next = words[i + 1].toCharArray();
  354. int len = Math.min(cur.length, next.length);
  355. int j = 0;
  356. for (; j < len; j++) {
  357. if (cur[j] != cur[j]) {
  358. if (!graph.containsKey(cur[j])) {
  359. graph.put(cur[j], new HashSet<>());
  360. }
  361. if (!graph.get(cur[j]).contains(next[j])) {
  362. graph.get(cur[j]).add(next[j]);
  363. indegree.put(next[j], indegree.get(next[j]) + 1);
  364. }
  365. break;
  366. }
  367. }
  368. if (j < cur.length && j == next.length) {
  369. return "";
  370. }
  371. }
  372. StringBuilder ans = new StringBuilder();
  373. Queue<Character> queue = new LinkedList<>();
  374. for (Character key : indegree.keySet()) {
  375. if (indegree.get(key) == 0) {
  376. queue.offer(key);
  377. }
  378. }
  379. while (!queue.isEmpty()) {
  380. char cur = queue.poll();
  381. ans.append(cur);
  382. if (graph.containsKey(cur)) {
  383. for (char next : graph.get(cur)) {
  384. indegree.put(next, indegree.get(next) - 1);
  385. if (indegree.get(next) == 0) {
  386. queue.offer(next);
  387. }
  388. }
  389. }
  390. }
  391. return ans.length() == indegree.size() ? ans.toString() : "";
  392. }