|
1 | 1 | package com.blankj.hard._044;
|
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | + |
3 | 7 | /**
|
4 | 8 | * <pre>
|
5 | 9 | * author: Blankj
|
@@ -30,35 +34,74 @@ public class Solution {
|
30 | 34 | // return pi == pl;
|
31 | 35 | // }
|
32 | 36 |
|
33 |
| - public boolean isMatch(String s, String p) { |
34 |
| - if (p.length() == 0) return s.length() == 0; |
35 |
| - int sl = s.length(), pl = p.length(); |
36 |
| - boolean[][] dp = new boolean[sl + 1][pl + 1]; |
37 |
| - char[] sc = s.toCharArray(), pc = p.toCharArray(); |
38 |
| - dp[0][0] = true; |
39 |
| - for (int i = 1; i <= pl; ++i) { |
40 |
| - if (pc[i - 1] == '*') dp[0][i] = dp[0][i - 1]; |
| 37 | +// public boolean isMatch(String s, String p) { |
| 38 | +// if (p.length() == 0) return s.length() == 0; |
| 39 | +// int sl = s.length(), pl = p.length(); |
| 40 | +// boolean[][] dp = new boolean[sl + 1][pl + 1]; |
| 41 | +// char[] sc = s.toCharArray(), pc = p.toCharArray(); |
| 42 | +// dp[0][0] = true; |
| 43 | +// for (int i = 1; i <= pl; ++i) { |
| 44 | +// if (pc[i - 1] == '*') dp[0][i] = dp[0][i - 1]; |
| 45 | +// } |
| 46 | +// for (int i = 1; i <= sl; ++i) { |
| 47 | +// for (int j = 1; j <= pl; ++j) { |
| 48 | +// if (pc[j - 1] != '*') { |
| 49 | +// dp[i][j] = dp[i - 1][j - 1] && (sc[i - 1] == pc[j - 1] || pc[j - 1] == '?'); |
| 50 | +// } else { |
| 51 | +// dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; |
| 52 | +// } |
| 53 | +// } |
| 54 | +// } |
| 55 | +// return dp[sl][pl]; |
| 56 | +// } |
| 57 | + |
| 58 | + public List<String> fullJustify(String[] words, int maxWidth) { |
| 59 | + int len = words.length; |
| 60 | + if (len == 0) return Collections.emptyList(); |
| 61 | + List<String> ans = new ArrayList<>(); |
| 62 | + StringBuilder spaces = new StringBuilder(); |
| 63 | + for (int i = 0; i < maxWidth; ++i) { |
| 64 | + spaces.append(" "); |
41 | 65 | }
|
42 |
| - for (int i = 1; i <= sl; ++i) { |
43 |
| - for (int j = 1; j <= pl; ++j) { |
44 |
| - if (pc[j - 1] != '*') { |
45 |
| - dp[i][j] = dp[i - 1][j - 1] && (sc[i - 1] == pc[j - 1] || pc[j - 1] == '?'); |
| 66 | + int sLen = -1, left = 0; |
| 67 | + for (int i = 0; i < len; ++i) { |
| 68 | + if (sLen + words[i].length() + 1 <= maxWidth) { |
| 69 | + sLen += words[i].length() + 1; |
| 70 | + } else { |
| 71 | + StringBuilder sub = new StringBuilder(words[left]); |
| 72 | + int rest = maxWidth - sLen; |
| 73 | + int seg = i - left; |
| 74 | + if (seg == 0) { |
| 75 | + sub.append(spaces.substring(0, rest)); |
46 | 76 | } else {
|
47 |
| - dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; |
| 77 | + int leastSpace = rest / seg + 1; |
| 78 | + int restSpace = rest % seg; |
| 79 | + for (int j = left + 1; j < i; ++j) { |
| 80 | + if (restSpace-- > 0) { |
| 81 | + sub.append(spaces.substring(0, leastSpace + 1)).append(words[j]); |
| 82 | + } else { |
| 83 | + sub.append(spaces.substring(0, leastSpace)).append(words[j]); |
| 84 | + } |
| 85 | + } |
48 | 86 | }
|
| 87 | + ans.add(sub.toString()); |
| 88 | + left = i; |
| 89 | + sLen = words[i].length(); |
49 | 90 | }
|
50 | 91 | }
|
51 |
| - return dp[sl][pl]; |
| 92 | + StringBuilder sub = new StringBuilder(words[left]); |
| 93 | + for (int i = left + 1; i < len; ++i) { |
| 94 | + sub.append(" ").append(words[i]); |
| 95 | + } |
| 96 | + ans.add(sub + spaces.substring(0, maxWidth - sub.length())); |
| 97 | + return ans; |
52 | 98 | }
|
53 | 99 |
|
| 100 | + |
54 | 101 | public static void main(String[] args) {
|
55 | 102 | Solution solution = new Solution();
|
56 |
| - System.out.println(solution.isMatch("aa", "a")); // false |
57 |
| - System.out.println(solution.isMatch("aa", "aa")); // true |
58 |
| - System.out.println(solution.isMatch("aaa", "aa")); // false |
59 |
| - System.out.println(solution.isMatch("aa", "*")); // true |
60 |
| - System.out.println(solution.isMatch("aa", "a*")); // true |
61 |
| - System.out.println(solution.isMatch("ab", "?*")); // true |
62 |
| - System.out.println(solution.isMatch("aab", "c*a*b"));// false |
| 103 | + System.out.println(solution.fullJustify(new String[]{"", ""}, 0)); |
| 104 | + System.out.println(solution.fullJustify(new String[]{"a"}, 1)); |
| 105 | + System.out.println(solution.fullJustify(new String[]{"This", "is", "an", "example", "of", "text", "justification."}, 16)); |
63 | 106 | }
|
64 | 107 | }
|
0 commit comments