import React from 'react'; import type { ProblemItem, ParsedExample, ProblemSolution } from '../types/reviewTypes'; export function parseProblemContent(problem: ProblemItem | null | undefined): { description: string; examples: ParsedExample[]; constraints: string[]; } { if (!problem) return { description: '', examples: [], constraints: [] }; let description = problem.description || ''; let examples: ParsedExample[] = []; let constraints: string[] = []; // 1. If problem already has structured examples if (Array.isArray(problem.examples) && problem.examples.length > 0) { examples = problem.examples.map((ex) => { if (typeof ex === 'string') { return { input: ex, output: '' }; } return { input: ex?.input || '', output: ex?.output || '', explanation: ex?.explanation || '' }; }); } // 2. If problem already has structured constraints if (Array.isArray(problem.constraints) && problem.constraints.length > 0) { constraints = problem.constraints.map((c) => String(c).replace(/^[\u2022\-*]\s*/, '').trim()).filter(Boolean); } else if (typeof problem.constraints === 'string' && problem.constraints.trim()) { constraints = problem.constraints.split('\n').map((c) => c.replace(/^[\u2022\-*]\s*/, '').trim()).filter(Boolean); } // 3. Fallback: Parse raw description if needed if (examples.length === 0 || constraints.length === 0) { const exMatch = description.search(/\bExample\s*\d*:/i); const coMatch = description.search(/\bConstraints\s*:/i); let firstSplit = -1; if (exMatch !== -1 && coMatch !== -1) { firstSplit = Math.min(exMatch, coMatch); } else if (exMatch !== -1) { firstSplit = exMatch; } else if (coMatch !== -1) { firstSplit = coMatch; } if (firstSplit !== -1) { const cleanDesc = description.substring(0, firstSplit).trim(); const bodyRest = description.substring(firstSplit); let exPart = bodyRest; let coPart = ''; const coInRest = bodyRest.search(/\bConstraints\s*:/i); if (coInRest !== -1) { exPart = bodyRest.substring(0, coInRest).trim(); coPart = bodyRest.substring(coInRest).replace(/\bConstraints\s*:/i, '').trim(); } if (examples.length === 0 && exPart) { const regex = /\bExample\s*(\d*)\s*:/gi; const matches: { index: number; text: string }[] = []; let m; while ((m = regex.exec(exPart)) !== null) { matches.push({ index: m.index, text: m[0] }); } for (let i = 0; i < matches.length; i++) { const start = matches[i].index + matches[i].text.length; const end = i + 1 < matches.length ? matches[i + 1].index : exPart.length; const block = exPart.substring(start, end).trim(); const inpMatch = block.match(/Input:\s*([\s\S]+?)(?=(?:Output:|Explanation:|$))/i); const outMatch = block.match(/Output:\s*([\s\S]+?)(?=(?:Explanation:|$))/i); const expMatch = block.match(/Explanation:\s*([\s\S]+)/i); const inp = inpMatch ? inpMatch[1].trim() : ''; const out = outMatch ? outMatch[1].trim() : ''; const exp = expMatch ? expMatch[1].trim() : ''; if (inp || out) { examples.push({ input: inp, output: out, explanation: exp }); } else if (block) { examples.push({ input: block, output: '', explanation: '' }); } } } if (constraints.length === 0 && coPart) { constraints = coPart .split('\n') .map((l) => l.replace(/^[\u2022\-*]\s*/, '').trim()) .filter(Boolean); } if (examples.length > 0 || constraints.length > 0) { description = cleanDesc; } } } return { description, examples, constraints }; } import { renderInlineMarkdown } from './markdownUtils'; import { MarkdownRenderer } from '../components/MarkdownRenderer'; export { renderInlineMarkdown }; export function parseMarkdown(text: string): React.ReactNode[] { if (!text) return []; return []; } export interface ParsedProblemTitle { cleanTitle: string; approachVariant: string | null; pattern: string; } /** * Normalizes problem title, extracts approach variants (e.g. "Hash Map (One-Pass)"), * and resolves the canonical algorithm pattern, preventing repetitive "Method 1" echoes. */ export function parseProblemTitle( rawName: string | undefined | null, catalogName?: string | null, rawPattern?: string | null, catalogPattern?: string | null ): ParsedProblemTitle { const isGeneric = (str?: string | null) => !str || /^Method\s*\d*$/i.test(str.trim()) || str.trim().toLowerCase() === 'standard'; let name = (rawName || catalogName || 'Algorithm Problem').trim(); let approachVariant: string | null = null; // 1. If catalogName is provided and name starts with catalogName, extract the suffix if (catalogName && name.toLowerCase().startsWith(catalogName.toLowerCase())) { const suffix = name.slice(catalogName.length).trim(); name = catalogName; // Canonical clean title if (suffix) { // Check for: // " - Method 1 (Hash Map (One-Pass))" // " - Method 1 (Method 1)" const methodWithParenMatch = suffix.match(/^[-\s]*Method\s*(\d+)\s*\((.+)\)$/i); if (methodWithParenMatch) { const methodNum = parseInt(methodWithParenMatch[1], 10); const parenContent = methodWithParenMatch[2].trim(); if (!isGeneric(parenContent)) { approachVariant = parenContent; } else if (methodNum > 1) { approachVariant = `Method ${methodNum}`; } } else { const methodOnlyMatch = suffix.match(/^[-\s]*Method\s*(\d+)/i); if (methodOnlyMatch) { const methodNum = parseInt(methodOnlyMatch[1], 10); if (methodNum > 1) { approachVariant = `Method ${methodNum}`; } } else { const parenMatch = suffix.match(/^[-\s]*\((.+)\)$/); if (parenMatch) { const parenContent = parenMatch[1].trim(); if (!isGeneric(parenContent)) { approachVariant = parenContent; } } else { const hyphenMatch = suffix.match(/^[-\s]+([^-]+)$/); if (hyphenMatch && !isGeneric(hyphenMatch[1])) { approachVariant = hyphenMatch[1].trim(); } } } } } } else { // 2. Fallback when catalogName is not available or doesn't match prefix const methodParenRegex = /^(.*?)\s*-\s*Method\s*(\d+)\s*\((.+)\)$/i; const methodParenMatch = name.match(methodParenRegex); if (methodParenMatch) { name = methodParenMatch[1].trim(); const methodNum = parseInt(methodParenMatch[2], 10); const parenContent = methodParenMatch[3].trim(); if (!isGeneric(parenContent)) { approachVariant = parenContent; } else if (methodNum > 1) { approachVariant = `Method ${methodNum}`; } } else { const methodRegex = /^(.*?)\s*-\s*Method\s*(\d+)$/i; const methodMatch = name.match(methodRegex); if (methodMatch) { name = methodMatch[1].trim(); const methodNum = parseInt(methodMatch[2], 10); if (methodNum > 1) { approachVariant = `Method ${methodNum}`; } } else { const parenMethodRegex = /^(.*?)\s*\(\s*Method\s*(\d+)\s*\)$/i; const parenMethodMatch = name.match(parenMethodRegex); if (parenMethodMatch) { name = parenMethodMatch[1].trim(); const methodNum = parseInt(parenMethodMatch[2], 10); if (methodNum > 1) { approachVariant = `Method ${methodNum}`; } } } } } // Determine resolved pattern: let resolvedPattern = 'General Algorithm'; if (!isGeneric(rawPattern)) { resolvedPattern = rawPattern!.trim(); } else if (!isGeneric(catalogPattern)) { resolvedPattern = catalogPattern!.trim(); } else if (approachVariant && !isGeneric(approachVariant)) { resolvedPattern = approachVariant; } return { cleanTitle: name, approachVariant, pattern: resolvedPattern }; } /** * Normalizes problem solutions into a clean ProblemSolution[] array. * If the problem has a dedicated solutions array, it uses it. * Otherwise, it creates fallback solutions from reference_code, pattern, optimal_time, optimal_space, and userSolution. */ export function normalizeProblemSolutions( problem: ProblemItem | null | undefined, officialSolution?: string, userSolution?: string ): ProblemSolution[] { if (!problem) return []; if (Array.isArray(problem.solutions) && problem.solutions.length > 0) { return problem.solutions; } const results: ProblemSolution[] = []; const primaryCode = officialSolution || problem.reference_code || ''; const pattern = problem.pattern || 'General Algorithm'; const timeComplexity = problem.optimal_time || 'O(N)'; const spaceComplexity = problem.optimal_space || 'O(1)'; if (primaryCode || pattern) { results.push({ id: 'sol-1', name: problem.approach_variant || 'Optimal Solution', pattern, time_complexity: timeComplexity, space_complexity: spaceComplexity, code: primaryCode, explanation: problem.how_to_solve || '', is_optimal: true }); } // If user has a distinct personal solution not matching official, add as Solution 2 if (userSolution && userSolution.trim() && userSolution.trim() !== primaryCode.trim()) { results.push({ id: 'sol-2', name: 'Personal Solution', pattern, time_complexity: timeComplexity, space_complexity: spaceComplexity, code: userSolution, is_optimal: false }); } return results; }