Give me code in java according to the image
تم الإنشاء في: ١٢ يوليو ٢٠٢٥
تم الإنشاء في: ١٢ يوليو ٢٠٢٥
pythonimg = Image.open('/mnt/data/IMG-20250712-WA0024.jpg') img.size (1280, 720)
pythonleft = img.crop((0,0,640,720)) # try OCR text = pytesseract.image_to_string(left) text '0) SECTION Geman Soaeig\n\n: Pontribute to the standord output ~\n\nConstraints:\nae | Vetety=t00\n\non\nSingle line of input contains str\nOutput Fon\n\nSingle fine of output\nfrom left to right and it\n\nre ans. the frst Cecuring anagram when itis pro\nformed. If the\n\nitis the shortest anay ol are 8\nsi nagram by which allother ahboranne ee\nGiven word is not a multigram, auinene .\n\nSample input 1;\ncece:\n\nSample Output:\n\nExplanation:\nGiven word str is “ccec’,t is a multigram. Anagram of ‘cc’ gives the desited\n\nalthough the shortest anagram is c\nSample input2:\nXYYYAYYY\nSample Output 2:\nxy\nExplanation:\nGiven word str is xyyyxyyyx’itis a multigran\njagrams of xyy can formyay and yes\nThe first occuring anagram when it is processe\n\nthe shortest anagram by which all other ar\n\n'
pythonprint(text)
pythonright = img.crop((640,0,1280,720)) text2 = pytesseract.image_to_string(right) print(text2)
pythonimport matplotlib.pyplot as plt plt.imshow(img) plt.axis('off') (-0.5, 1279.5, 719.5, -0.5)
pythonplt.figure(figsize=(6,10)) plt.imshow(left) plt.axis('off') (-0.5, 639.5, 719.5, -0.5)
pythonlower = img.crop((0,360,640,720)) plt.figure(figsize=(6,10)) plt.imshow(lower) plt.axis('off') (-0.5, 639.5, 359.5, -0.5)
pythonupper = img.crop((0,100,640,360)) plt.figure(figsize=(6,10)) plt.imshow(upper) plt.axis('off') (-0.5, 639.5, 259.5, -0.5)
Below is a compact, self-contained solution in Java that follows the exact skeleton you have on-screen:
javaimport java.io.*; import java.util.*; public class TestClass { /*------------------------------------------------------------ * Returns the “shortest anagram” that can generate the whole * string as a concatenation of its (possibly permuted) copies. * If no such base string exists (i.e. str is NOT a multigram), * the function returns "-1". *-----------------------------------------------------------*/ public static String shortestAnagram(String str) { int n = str.length(); int[] freq = new int[26]; // letter counts for (char ch : str.toCharArray()) freq[ch - 'a']++; /*----------- find the GCD of all non-zero counts --------*/ int g = 0; for (int f : freq) if (f > 0) g = g == 0 ? f : gcd(g, f); /* g == 1 ➜ cannot split into ≥2 equal multisets */ if (g <= 1) return "-1"; int m = n / g; // length of base word int[] target = new int[26]; // counts in base word for (int i = 0; i < 26; i++) target[i] = freq[i] / g; /*----- slide a window of length m to find first anagram -*/ int[] window = new int[26]; char[] s = str.toCharArray(); for (int i = 0; i < n; i++) { window[s[i] - 'a']++; if (i >= m) window[s[i - m] - 'a']--; if (i >= m - 1 && same(window, target)) return str.substring(i - m + 1, i + 1); // first hit } return "-1"; // should never happen if g > 1 } /*------------- helpers ------------------------------------*/ private static int gcd(int a, int b) { while (b != 0) { int t = a % b; a = b; b = t; } return a; } private static boolean same(int[] a, int[] b) { for (int i = 0; i < 26; i++) if (a[i] != b[i]) return false; return true; } /*------------------- driver code ---------------------------*/ public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine().trim().toLowerCase(); // assumes lower-case input System.out.println(shortestAnagram(str)); } }
Detecting a multigram –
The word is a multigram iff every character count shares a common divisor ≥ 2.
We compute that divisor with a plain GCD over the frequency table.
Finding the shortest base word –
If the divisor is g
, the base word length is |str| / g
, and its character multiset is freq / g
.
Printing the first occurring anagram –
Using a sliding window of that length, we scan left-to-right; the first window whose multiset equals the target multiset is printed, exactly as required by “first occurring from left to right”.
Edge cases –
Not a multigram → output -1
; constraints 1 < |str| ≤ 200
easily fit into this O(26·|str|)
approach.
Give me code in cpp
Here’s a straightforward C++ program that mirrors the logic of the Java version I sent earlier:
cpp#include <bits/stdc++.h> using namespace std; /*-------------------------------------------------------------- * Returns the shortest anagram that can tile (re-concatenate to) * the whole string. If the word is *not* a multigram, prints –1. *-------------------------------------------------------------*/ string shortestAnagram(const string &str) { int n = str.size(); array<int,26> freq{}; // letter frequencies for (char ch : str) freq[ch - 'a']++; /*-------------- compute the GCD of all positive counts -------------*/ auto gcd = [](int a, int b){ while (b) { int t = a % b; a = b; b = t; } return a; }; int g = 0; for (int f : freq) if (f) g = g ? gcd(g, f) : f; if (g <= 1) return "-1"; // not a multigram int baseLen = n / g; // length of base word array<int,26> target{}; // char multiset of base for (int i = 0; i < 26; ++i) target[i] = freq[i] / g; /*------------ sliding-window search for first occurrence -----------*/ array<int,26> window{}; for (int i = 0; i < n; ++i) { window[str[i] - 'a']++; if (i >= baseLen) // pop from left if window too big window[str[i - baseLen] - 'a']--; if (i >= baseLen - 1 && window == target) return str.substr(i - baseLen + 1, baseLen); } return "-1"; // logically unreachable if g>1 } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string s; if (getline(cin, s)) { for (char &c : s) c = tolower(c); // assume case-insensitivity cout << shortestAnagram(s) << '\n'; } return 0; }
freq
.g
).g ≤ 1
, the word cannot be split into repeated anagrams → output -1
.freq / g
, base length = n / g
.Complexity: O(26 · |str|)
time, O(26)
extra space — easily fits the constraint |str| ≤ 200
.